I had a large Docker Compose project on a Proxmox LXC and I recently decided to migrate that to a new VM. So, in addition to code, I had to create an exact copy of my Docker volume and non-volume data. Here’s how I did that.

First, a few things to note:

  • I used the destination VM to trigger the rsync command.
  • I used sudo on both sides because the files were owned by multiple arbitrary users. (This complicated things a bit.)
  • I had to do the following in order. (Earlier, I didn’t docker compose create and ran into this error.)
    • rsync the Docker Compose code.
    • docker compose create on the destination.
    • rsync Docker volume data. (I did this twice actually: once, while the project was up on the source and once after bringing it down. This helped me decrease overall downtime.)
    • docker compose up -d or whatever on the destination.

Second, I used something like the following to create exact copies:

sudo rsync \
	-e "ssh -i /home/arch/.ssh/id_ed25519" \
	-aPHAX \
	--numeric-ids \
	--delete \
	--rsync-path="sudo /usr/bin/rsync" \
	--exclude='docker_foo/***' \
	--include='docker_*/***' \
	--exclude='*' \
	user@src_host:/var/lib/docker/volumes/ /var/lib/docker/volumes/

Here, the ordering of --exclude and --include was important.