How to Migrate Docker Files and Volumes Between Servers Using rsync
Updated On: Dec. 05, 2024 Author: Kevin

How to Migrate Docker Files and Volumes Between Servers Using rsync

When moving Docker workloads to a new server, one essential task is ensuring all Docker-related data is transferred. This includes container configurations, images, and volumes. Docker volumes store persistent data that containers use, making them crucial for smooth application migrations.

In this guide, we’ll walk you through migrating all Docker files—including volumes, images, and containers—between servers using rsync. We’ll also cover how to install rsync, ensure it’s available on both systems, and resolve sudo password issues. Remember to always backup when doing things like this.

Prerequisites

  • Two servers: source and destination.
  • SSH access to both servers.
  • sudo privileges on both servers.

Step 1: Install rsync on Both Servers

First, ensure rsync is installed on both servers. Use the following commands:

On Debian/Ubuntu:

sudo apt update
sudo apt install rsync

On CentOS/RHEL:

sudo yum install rsync

On Fedora:

sudo dnf install rsync

Step 2: Stop Docker on Both Servers

To prevent data inconsistencies during migration, stop Docker on both the source and destination servers:

sudo systemctl stop docker

Step 3: Migrate Docker Volumes

Docker volumes are typically stored in /var/lib/docker/volumes. If you need to find the locations of volumes use this command sudo docker inpect <container-name or ID> | grep -i source

 

Use the following command to migrate volumes from the source server to the destination server: 

sudo rsync -aP --rsync-path="sudo rsync" user@source-server:/var/lib/docker/volumes/ /var/lib/docker/volumes/

Here’s what the options mean:

  • -a: Preserve file permissions and timestamps.
  • -P: Show progress and allow resuming of partial transfers.
  • --rsync-path="sudo rsync": Run rsync as sudo on the source server to access protected directories.

Step 4: Migrate Docker Images and Containers

To transfer Docker images and containers, you need to back up the entire /var/lib/docker directory:

sudo rsync -aP --rsync-path="sudo rsync" user@source-server:/var/lib/docker/ /var/lib/docker/

This command copies all Docker data, including images, container metadata, and volumes. If you’ve already migrated volumes, you can exclude them:

sudo rsync -aP --rsync-path="sudo rsync" --exclude="volumes" user@source-server:/var/lib/docker/ /var/lib/docker/

Step 5: Fix Permissions on the Destination Server

After transferring the files, ensure proper ownership and permissions on the destination server:

sudo chown -R root:root /var/lib/docker

Step 6: Restart Docker

Start Docker on both servers to ensure everything is working correctly:

sudo systemctl start docker

Step 7: Test Your Migration

Verify that your containers and volumes are accessible on the destination server:

docker ps -a
docker volume ls

Handling sudo Password Prompts

If you encounter issues with sudo requiring a password during rsync, configure passwordless sudo for rsync on the source server:

  1. Edit the sudoers file on the source server:
sudo visudo
  1. Add the following line, replacing user with your username:
user ALL=(ALL) NOPASSWD: /usr/bin/rsync

This allows rsync to run with sudo privileges without prompting for a password.

Conclusion

Using rsync is an efficient way to migrate all Docker files, including volumes, images, and container data, between servers. With the steps outlined in this guide, you can ensure a smooth transition for your Docker workloads.