This article was published over 2 years ago. Some information may be outdated.
Backing up Docker Swarm is not optional. If you are running Swarm in production and you do not have backups, you are one incident away from rebuilding everything from scratch.
Before we begin, here are the key things to know:
- This guide assumes Ubuntu or a similar Linux distribution.
- Swarm configurations are stored in
/var/lib/docker/swarm. - You can use any manager node to take the backup. If you have three managers, any one of them works.
- Avoid using the leader-manager node for backups. Use another manager instead.
- Schedule regular backups. Do not rely on manual runs.
Creating the Backup
Stop Docker first:
sudo service docker stop
Compress the /var/lib/docker/swarm directory into a tarball:
tar -czvf swarm.backup.tar /var/lib/docker/swarm/
You must stop Docker before taking the backup. Taking a backup while Docker is running risks capturing an inconsistent state.
The backup is now ready.
Restoring the Backup
To restore, stop Docker, remove the existing swarm directory, and extract the tarball:
service docker stop
rm -Rf /var/lib/docker/swarm
tar -zxvf swarm.backup.tar -C /var/lib/docker
After extracting, you need to re-initialize Swarm. The --force-new-cluster flag makes this possible without losing the restored configuration:
docker swarm init --force-new-cluster
This re-initializes the Swarm using the restored data. Your services, networks, and secrets will be intact.
Summary
- Stop Docker before backing up. Running backups against a live Swarm directory risks data corruption.
- The backup target is
/var/lib/docker/swarm. This single directory contains all Swarm state. - Use any manager node except the leader. Any manager has the full Raft log, but avoiding the leader reduces risk.
- Restore requires
--force-new-cluster. After extracting the backup, this flag re-initializes the cluster from the restored state. - Automate your backups. A backup you have to remember to run is a backup you will eventually forget.