Today I Learned: 23/11/2021 - Copying in-use Docker volume files across hosts
I needed to move an existing MySQL Docker container to a different host, including the volumed-in contents of the /var/lib/mysql directory. In theory, it should be possible to scp the files from the volume’s directory on the host to a volume directory on the destination host, but previous attempts to do that haven’t worked for me.
I found a very clever one line command here which did exactly what I was after:
docker run --rm -v <SOURCE_DATA_VOLUME_NAME>:/from alpine ash -c "cd /from ; tar -cf - . " | ssh <TARGET_HOST> 'docker run --rm -i -v <TARGET_DATA_VOLUME_NAME>:/to alpine ash -c "cd /to ; tar -xpvf - " '
Digging into the this command:
- The source host is sshed to before starting
- A container from the small and basic alpine image is spun up on the source host
- The volume to be copied is volumed in to /from
- A command in the container is run to tar up all the contents of the /from directory and output the resulting bytes to standard out
- The bytes are piped into an ssh session running on the destination host, executing a command to run the same alpine image
- The volume to copy to is volumed in to /to
- A command in the container is run to untar the bytes coming in on standard in to the /to directory
This solution is both elegant and smart - I’m grateful to the author for teaching me a few new tricks with it.