Creating a volume in a dockerfile without a persistent volume (claim) in Kubernetes?

7/11/2019

I have an application that I am converting into a docker container.

I am going to test some different configuration for the application regarding persisted vs non persisted storage.

E.g. in one scenario I am going to create a persisted volume and mount some data into that volume.

In another scenario I am going to test not having any persisted volume (and accept that any date generated while the container is running is gone when its stopped/restarted).

Regarding the first scenario that works fine. But when I am testing the second scenario - no persisted storage - I am not quite sure what to do on the docker side.

Basically does it make any sense do define a volume in my Dockerfile when I don't plan to have any persisted volumes in kubernetes?

E.g. here is the end of my Dockerfile

...
ENTRYPOINT ["./bin/run.sh"]
VOLUME /opt/application-x/data

So does it make any sense at all to have the last line when I don't create and kubernetes volumes?

Or to put it in another way, are there scenarios where creating a volume in a dockerfile makes sense even though no corresponding persistent volumes are created?

-- u123
docker
kubernetes

1 Answer

7/11/2019

It usually doesn’t make sense to define a VOLUME in your Dockerfile.

You can use the docker run -v option or Kubernetes’s container volume mount setting on any directory in the container filesystem space, regardless of whether or not its image originally declared it as a VOLUME. Conversely, a VOLUME can leak anonymous volumes in an iterative development sequence, and breaks RUN commands later in the Dockerfile.

In the scenario you describe, if you don’t have a VOLUME, everything is straightforward: if you mount something on to that path, in either plain Docker or Kubernetes, storage uses the mounted volume, and if not, data stays in the container filesystem and is lost when the container exits (which you want). I think if you do have a VOLUME then the container runtime will automatically create an anonymous volume for you; the overall behavior will be similar (it’s hard for other containers to find/use the anonymous volume) but in plain Docker at least you need to remember to clean it up.

-- David Maze
Source: StackOverflow