1

As I understand docker volumes allow to specify directory/file in docker container that will be shared/stored on docker host. For example postgres Dockerfile contains following line

VOLUME /var/lib/postgresql/data

So it means that /var/lib/postgresql/data will be actually "stored" on host and I will have access to that file(s) from a host system. For example when I inspect pg container:

"Mounts": [
        {
            "Name": "4fc1fe18d93fd2090f63619edc5d6244e9821805a2e070a0235d9305b2dfe80f",
            "Source": "/mnt/sda1/var/lib/docker/volumes/4fc1fe18d93fd2090f63619edc5d6244e9821805a2e070a0235d9305b2dfe80f/_data",
            "Destination": "/var/lib/postgresql/data",
            "Driver": "local",
            "Mode": "",
            "RW": true
        }
    ],

This means that I can find volume on host under Source path. I was wondering what means 4fc1fe18d93fd2090f63619edc5d6244e9821805a2e070a0235d9305b2dfe80f in the Source path. I can't find neither container nor image with such id on my docker host.

One thing I can't understand is that it seems that when I remove container and recreate it, then Source path is different, and in fact no data persist...

So it seems that for data persistance I have to add volume with both host and container paths to force it to store data always under same path - is this correct?

user606521
  • 14,486
  • 30
  • 113
  • 204

2 Answers2

1

Following "How do you list volumes in docker containers?", I have written a script (updateDataContainerPath) which:

  • either records that path in a file (named after the container)
  • or, if that file already exist, will:
    • delete Mount.Source folder (which is empty)
    • move the path recorded in the file to Mount.Source path
    • record Mount.Source path in that file

That way, I can delete (docker rm, without the -v option, obviously) a container and recreate it, while keeping the data put in that volume.

You can see that script used in gitolite/run for instance.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • That's cool - but I can also just specify fixed host path to achieve the same right? – user606521 Aug 20 '15 at 08:24
  • @user606521 I don't know, I preferred not to tweak with that generated path, and always keep the one provided by a `docker run` or `docker create`. – VonC Aug 20 '15 at 08:25
  • @user606521 did you try the other option (specify a fixed host path)? – VonC Aug 21 '15 at 09:13
  • @user606521 how did you change that path in the container? – VonC Aug 21 '15 at 09:22
  • I've just created container with following volume `/var/lib/api/postgresql/data:/var/lib/postgresql/data` - it's `:` - and now all files from `` are visible on host at ``. – user606521 Aug 21 '15 at 09:31
  • @user606521 OK. You are using a data volume (https://docs.docker.com/userguide/dockervolumes/#data-volumes). My script is for using (and preserving) a data volume container (https://docs.docker.com/userguide/dockervolumes/#creating-and-mounting-a-data-volume-container) – VonC Aug 21 '15 at 09:33
1

So it seems that for data persistance I have to add volume with both host and container paths to force it to store data always under same path - is this correct?

Correct. That is called "mounting a host directory as a data volume". Documentation can be found here.

But it can be achieved with something like:

docker run -v /path/to/host/directory:/path/inside/the/container image

Other option is using a "data volume container". In this way, you bind your container(s) into a data volume container. You can re create your container(s), and as long as you don't remove the data volume container, data will be persisted. Documentation can be found here.

Luís Bianchin
  • 2,327
  • 1
  • 28
  • 36