user606521
user606521

Reputation: 15454

Docker volumes for persistent data - is it enough to pass container path only?

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?

Upvotes: 1

Views: 4180

Answers (2)

VonC
VonC

Reputation: 1327764

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.

Upvotes: 1

Luís Bianchin
Luís Bianchin

Reputation: 2496

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.

Upvotes: 1

Related Questions