As described in the Docker documentation on Working with Volumes there is the concept of so-called data-only containers, which provide a volume that can be mounted into multiple other containers, no matter whether the data-only container is actually running or not.
Basically, this sounds awesome. But there is one thing I do not understand.
These volumes (which do not explicitly map to a folder on the host for portability reasons, as the documentation states) are created and managed by Docker in some internal folder on the host (/var/docker/volumes/…
).
Supposed I use such a volume, and then I need to migrate it from one host to another - how do I port the volume? AFAICS it has a unique ID - can I just go and copy the volume and its according data-only container to a new host? How do I find out which files to copy? Or is there some support built-in to Docker that I did not discover yet?
Extending the official answer from Docker docs and the top answer here, you can have following aliases in your .bashrc or .zshrc
Note that the backup is saved into
/tmp
, so you can move the backup file saved there between docker hosts.There is also two pairs of backup/restore aliases. One using compression and debian:jessie and other with no compression but with busybox. Favor using compression if the files to backup are big.
In case your machines are in different VPCs or you want to copy from/to local machine (like in my case) you can use dvsync I created. It's basically ngrok combined with
rsync
over SSH packaged into two small (both ~25MB) images. First, you start thedvsync-server
on a machine you want to copy data from (You'll need theNGROK_AUTHTOKEN
which can be obtained from ngrok dashboard):Then you can start the
dvsync-client
on the machine you want to copy the files to, passing theDVSYNC_TOKEN
shown by the server:Once the copying will be done, the client will exit. This works with Docker CLI, Compose, Swarm and Kubernetes as well.
You can export the volume to tar and transfer to another machine. And import the data with tar on the second machine. This does not rely on implementation details of the volumes.
I'll add another recent tool here from IBM which is actually made for the volume migration from one container host to another. This is a currently on-going project. So, you may find a different version with additional features in future.
Cargo was developed to migrate containers from one host to another host along with their data with minimal downtime. Cargo uses data federation capabilities of union filesystem to create a unified view of data (mainly the root file system) across the source and target hosts. This allows Cargo to start up a container almost immediately (within milliseconds) on the target host as the data from source root file system gets copied to target hosts either on-demand (using a copy-on-write (COW) partition) or lazily in the background (using rsync).
Important points are: - a
centralized
server handles the migration processThe link to the project is given here:
The official answer is now available here:
Sharing Directories using Volumes
In the "Backup, restore, or migrate data volumes" section you have:
BACKUP:
--rm
: remove the container when it exits--volumes-from DATA
: attach to the volumes shared by the DATA container-v $(pwd):/backup
: bind mount the current directory into the container; to write the tar file tobusybox
: a small simpler image - good for quick maintenancetar cvf /backup/backup.tar /data
: creates an uncompressed tar file of all the files in the /data directoryRESTORE: