Volumes in Dockerfiles hide named volumes

Jun 18, 2019 00:00 · 142 words · 1 minute read docker container volume

So, what happens if you declare a volume inside your dockerfile AND as option when you start your container with docker run?

It turns out that declaring a volume inside the dockerfile makes docker create an anonymous volume on every run of the container. But what happens then to the volume that was specified with the docker run command?

It will be hidden by the anonymous volume! The effect is, that data is always stored inside the anonymous volume, while the volume specified with the command remains empty.

The next question is, why did I specify the volume twice? The mental model I had was that the declaration inside the dockerfile was more of a documentation, so that when looking at the dockerfile you would instantly see that the container uses a volume instead of having to inspect the running container itself.

If you want to get in touch with me, send me an email

longo.tomas@googlemail.com