Docker volumes for persistent data - is it enough

2020-02-06 17:24发布

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?

标签: docker
2条回答
干净又极端
2楼-- · 2020-02-06 17:56

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.

查看更多
不美不萌又怎样
3楼-- · 2020-02-06 18:07

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.

查看更多
登录 后发表回答