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?
Following "How do you list volumes in docker containers?", I have written a script (
updateDataContainerPath
) which: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.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:
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.