Docker volume backup and restore

You're right. Since you can have multiple containers with volumes on their own, you need to keep track which volume corresponds to which container. How to do that depends on your setup: I use the name -data for the data container, so it's obvious to which container a image belongs. That way it can be backed up like this:

VOLUME=`docker inspect $NAME-data | jq '.[0].Volumes["/path/in/container"]'`
tar -C $VOLUME . -czvf $NAME.tar.gz

Now you just need to rebuild your image and recreate your data container:

cat $NAME.tar.gz | docker run -name $NAME-data -v /path/in/container \
                              -i busybox tar -C /path/int/container -xzf -

So this means you need to backup:

  • Dockerfile
  • volume
  • volume path in container
  • name of the container the volume belongs to

Update: In the meanwhile I created a tool to backup containers and their volume(s) (container(s)): https://github.com/discordianfish/docker-backup and a backup image that can create backups and push them to s3: https://github.com/discordianfish/docker-lloyd


In newer Docker (tested in 1.9.1, build 9894698) you can use the cp command.

Here is an example how to copy a directory from the container to the host:

docker cp wordpress:/var/www/html backups/wordpress.`date +"%Y%m%d"`/

Here is an example how to copy a directory from the container to a tar file:

docker cp wordpress:/var/www/html - > backups/wordpress.`date +"%Y%m%d"`.tar

Last but not least an example how to copy a directory from the container to a tar.gz file:

docker cp wordpress:/var/www/html - | gzip > backups/wordpress.`date +"%Y%m%d"`.tar.gz