Reputation: 11798
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.
Upvotes: 0
Views: 164
Reputation: 263946
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.
Upvotes: 1
Reputation: 2130
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:
--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
).
Upvotes: 0