4

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.

DenCowboy
  • 13,884
  • 38
  • 114
  • 210

1 Answers1

4

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.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • I have to install the flocker plugin for this? – DenCowboy May 26 '16 at 15:39
  • 1
    @MaxC No, this is in docker by default. – VonC May 26 '16 at 15:41
  • I got: Error response from daemon: create my-jenkins-volume: create my-jenkins-volume: Error looking up volume plugin flocker: plugin not found after trying to create such a volume (I'm using a real docker, not machine, on ubuntu 14.04, version of docker is 1.11.1 – DenCowboy May 26 '16 at 15:43
  • @MaxC What happend if you do not use the `-d flocker`? I have edited the answer to remove the reference to that plugin. – VonC May 26 '16 at 15:45
  • thanks. than it works. did some small testing and the data of my container persists. Very cool. I can even 'cd' into the /var/lib/docker/volume/myvolume! I still have my data in the old way. Can I just perform a docker cp on 'old' container to my local machine and than past the folder in my /var/lib/docker/my-volume? – DenCowboy May 26 '16 at 15:56
  • + can you just 'scp' that /var/lib/docker/volume to other servers and use it with other containers? – DenCowboy May 26 '16 at 16:23
  • No, the idea is to mount an existing volume to other containers. – VonC May 26 '16 at 16:25
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/113057/discussion-between-maxc-and-vonc). – DenCowboy May 26 '16 at 16:28
  • @MaxC back at work (no chat). You can use your backup by creating a new empty volume, and copying your data into `/var/lib/docker/volumes/` – VonC May 27 '16 at 05:56
  • So just a docker copy of my existing a folder inside my existing container and paste in the the folder of my new volume (created by docker volume create ...) – DenCowboy May 27 '16 at 06:32
  • @MaxC no, I would only backup what is recorded in `/var/lib/docker/volumes/`. – VonC May 27 '16 at 06:37
  • so a copy of whats inside /var/lib/docker/volumes/a37dfd3/_data (old volume) to my new /var/lib/docker/volumes/my-new-volume/_data – DenCowboy May 27 '16 at 06:49
  • @MaxC Yes, in term of backup, that is what I would copy/restore. – VonC May 27 '16 at 06:50
  • Isn't it going to complain about my user? because it's user jenkins or nexus but this user doesn't exist on my host. and I'm only able to copy as root (otherwise I cannot access /var/lib/docker/volumes) – DenCowboy May 27 '16 at 06:52
  • 2
    @MaxC if you backup using tar (https://help.ubuntu.com/community/BackupYourSystem/TAR, http://superuser.com/a/838399/141), it will backup the tree with the relevant user and permissions. – VonC May 27 '16 at 06:55