1

What is the best way to persist containers data with docker? I would like to be able to retain some data and be able to get them back when restarting my container. I have read this interesting post but it does not exactly answer my question.

As far as I understand, I only have one option:

docker run -v /home/host/app:/home/container/app

This will mount the countainer folder onto the host.

Is there any other option? FYI, I don't use linking containers (--link )

Community
  • 1
  • 1
Florent Valdelievre
  • 1,546
  • 3
  • 20
  • 32

2 Answers2

2

Using volumes is the best way of handling data which you want to keep from a container. Using the -v flag works well and you shouldn't run into issues with this.

You can also use the VOLUME instruction in the Dockerfile which means you will not have to add any more options at run time, however they're quite tightly coupled with the specific container, you'd need to use docker start, rather than docker run to get the data back (or of course -v to the volume which was created in the past, likely in /var/ somewhere).

A common way of handling volumes is to create a data volume container with volumes defined by -v Then when you create your app container, use the --volumes-from flag. This will make your new container use the same volumes as the container you used the -v on (your data volume container). Of course this may seem like you're shifting the issue somewhere else.

This makes it quite simple to share volumes over multiple containers. Perhaps you have a container for your application, and another for logstash.

Marcus Hughes
  • 5,123
  • 1
  • 25
  • 39
  • Thanks Marcus, If my understanding is correct, -v /app is only useful if I use data volume containers (--volumes-from ). It won't retain any data. If i don't want to create volume data container, my only solution is to bind a container folder onto the host ? (-v /home/host/app:/home/container/app) Is it correct? It would have been nice to have a system which automatically create a volume on the host to retain data but without specifying the host mount point(as we need to make sure the host mount point exists) – Florent Valdelievre Oct 21 '14 at 09:10
  • All volumes will persist on the host filesystem until you delete the container. You can use no volumes at all and still have the data if you just start the same container ID again (although this isn't best practise). – Marcus Hughes Oct 21 '14 at 10:02
  • @FlorentValdelievre if you create a data container, then you automatically get a directory for the volume in the /var/lib/docker/volumes/ directory on the host. The advantage of the volume container is the portability aspect, i.e. you don't need to rely on a particular directory hierarchy being setup on the host system... which is exactly what you seem to want. The only caveat is the storage device for housing /var/lib/docker/volumes should have capacity for whatever you are putting in the volume – Amos Folarin Oct 21 '14 at 19:18
  • @AmosFolarin So, just like other containers can the data-container images be downloaded so that even the data stored in database in those containers will also be downloaded? – JavaTechnical Dec 15 '14 at 18:33
  • 2
    No. You would have to house the database in the containers file system. Convention is that you put the data outside the container. Maybe take a look at Flocker? https://docs.clusterhq.com/en/latest/ for volume migration saw a demo a few months back looks interesting – Amos Folarin Dec 16 '14 at 11:14
0

create a volume-container: this format of -v creates a volume, directory e.g. /var/lib/docker/volume/d3b0d5b781b7f92771b7342824c9f136c883af321a6e9fbe9740e18b93f29b69 which is still a bind mounted /container/path/vol

docker run -v /foo/bar/vol --name volbox ubuntu

I can now use this container, as my volume.

docker run --volumes-from volbox --name foobox ubuntu /bin/bash
root@foobox# ls /container/path/vol

Now, if I distribute these two containers, they will just work. The volume will always be available to foobox, regardless which host it is deployed to.

The snag of course comes if you don't want your storage to be in /var/lib/docker/volumes...

I suggest you take a look at some of the excellent post by Michael Crosby https://docs.docker.com/userguide/dockervolumes/ and the docker docs https://docs.docker.com/userguide/dockervolumes/

Amos Folarin
  • 2,059
  • 20
  • 18