Docker volume: backup

2019-04-11 10:43发布

问题:

I have created a volume container. And a container which mounts to en from the volume container.

Volume container:

docker run -d --name nexus-data nexus:1.0 echo "data-only container for Nexus"

My Nexus container:

docker run -d -p 8443:8443 -p 8081:8081  --name nexus --restart=always --volumes-from nexus-data nexus:1.0

So this is working fine. I'm able to delete and recreate my nexus-container without losing data. The volume container (which isn't in running state) is saving the data.

But the bottleneck of this approach is the volume-container. When I accidentally delete this container, all the data is gone. This can happen pretty fast because some useful commands which will delete stopped containers will also delete the volume containers.

So I want to create backups for my volume-containers. I tried to ways:

$ docker cp nexus:/this/folder/ /home/ubuntu/backup-folder

And

$ docker import nexus > /home/ubuntu/backup.tar

So I have one folder and one .tar in my home directory. Now I want to know what's the right approach to import one of these backups?

I read about the docker export command to create a 'new image' but I don't like this approach because my backup folder is pretty big.

回答1:

Simply don't use data volume container: since docker 1.9, you can use docker volume create instead. Docker now has volume commands.

The following example also creates the my-named-volume volume, this time using the docker volume create command.

$ docker volume create --name my-named-volume -o size=20GB
$ docker run -d -P \
  -v my-named-volume:/opt/webapp \
  --name web training/webapp python app.py

Those volumes are named, listed by docker volume ls, and you can backup /var/lib/docker/volumes.

With data volume container, you would need to memorize the path of the data within that container (docker inspect -f '{{ (index .Mounts 0).Source }}), write it to a file, and use that path when you create a new data volume container.
I used to do that with updateDataContainerPath.sh. See "Reattaching orphaned docker volumes".

I do not need that convoluted mechanism since docker 1.9 and named volumes.