0

I found the following code in the Dockerfile of official postgresql. https://github.com/docker-library/postgres/blob/master/11/Dockerfile

ENV PGDATA /var/lib/postgresql/data
RUN mkdir -p "$PGDATA" && chown -R postgres:postgres "$PGDATA" && chmod 777 "$PGDATA" # this 777 will be replaced by 700 at runtime (allows semi-arbitrary "--user" values)
VOLUME /var/lib/postgresql/data

I want to know what is the purpose of VOLUME in this regard.

VOLUME /var/lib/postgresql/data

As per my understanding it will create a new storage volume when we start a container and that storage volume will also be deleted permanently when the container is removed (docker stop contianerid; docker rm containeid)

Then if the data is not going to persist then why to use this. Because VOLUME are used if we want the data to persist.

My question is w.r.t what is its use if the postgres data is only going to remain only till the container is running and after that everything is wiped out. If i have done lot of work and in the end everything is gone then whats the use.

Santhosh
  • 9,965
  • 20
  • 103
  • 243
  • You want to read https://4sysops.com/archives/introduction-to-docker-bind-mounts-and-volumes/ – Markus Deibel Apr 04 '19 at 08:41
  • Possible duplicate of [Understanding "VOLUME" instruction in DockerFile](https://stackoverflow.com/questions/41935435/understanding-volume-instruction-in-dockerfile) – samthegolden Apr 04 '19 at 08:42
  • I am asking w.r.t to data persistance. Because its going to be temporary. I want to know its usage. This is different from mounting because in mounting its understood, but in this case its just going to create a volume with hash name and later delete it after container is removed – Santhosh Apr 04 '19 at 09:06

2 Answers2

1

As per my understanding it will create a new storage volume when we start a container and that storage volume will also be deleted permanently when the container is removed (docker stop contianerid; docker rm containeid)

If you run a container with the --rm option, anonymous volumes are deleted when the container exits. If you do not pass the --rm option when creating the container, then the -v option to docker container rm will also delete volumes. Otherwise, these anonymous volumes will persist after a stop/rm.

That said, anonymous volumes are difficult to manage since it's not clear which volume contains what data. Particularly with images like postgresql, I would prefer if they removed the VOLUME line from their Dockerfile, and instead provided a compose file that defined the volume with a name. You can see more about what the VOLUME line does and why it creates problems in my answer over here.

BMitch
  • 231,797
  • 42
  • 475
  • 450
  • you are right, whenever i run with --rm then the volume gets deleted immidiately after the container stops. But i havent tried the other way like -d or without --rm. But as you mentioned even if they exist its will be difficult to understand by their random names for what they are. – Santhosh Apr 04 '19 at 18:52
0

Your understanding of how volumes works is almost correct but not completely.

As you stated, when you create a container from an image defining a VOLUME, docker will indeed create an anonymous volume (i.e. with a random name).

When you stop/remove the container the volume itself will not be deleted and will still be accessible by the docker volume family of commands.

Indeed in order to remove a container and delete associated volumes you have to use the -v flag as in docker rm -v container-name. This command will remove the container and delete all the anonymous volumes associated with it (named volume will never be deleted unless explicitly requested via docker volume rm volume-name).

So to sum up the VOLUME directive inside a Dockerfile is used to identify those places that will host persistent data and ensuring the following:

  • data will survive the life of the container by default
  • data can be shared with other containers (i.e. --volumes-from)

The most important aspect to me is that it also serves as a sort of implicit documentation for your user to let them know where the persistent state is kept (so that they can name the volumes via the -v flag of docker run).

nivox
  • 2,060
  • 17
  • 18
  • `The most important aspect to me is that it also serves as a sort of implicit documentation for your user to let them know where the persistent state is kept (so that they can name the volumes via the -v flag of docker run).` indirectly this can be a good reason also. `--volumes-from` definitely sometimes. `data will survive the life of the container by default`: even thought the volume is still existing, but as mentioned by @BMitch `anonymous volumes are difficult to manage since it's not clear which volume contains what data.` – Santhosh Apr 04 '19 at 18:56
  • Anyhow i was confused mainly because i run my containers using --rm. then the volume is always deleted when the container stops. – Santhosh Apr 04 '19 at 18:57