Dockerfile
FROM nginx:1.7
# Deliberately declaring VOLUME before RUN
VOLUME /var/tmp
RUN touch /var/tmp/test.txt
Observation
Now I believe I understand the implications of declaring the VOLUME
statement before
creating test.txt
- the volume /var/tmp
exposed at runtime will be based on the intermediary container prior to the test.txt
file is created so it will be empty (hope this observation is correct)
So as expected the following docker run
does not show test.txt
:
docker run kz/test-volume:0.1
But then I tried supplying the volume at runtime as below:
docker run -v /var/tmp kz/test-volume:0.1
Question
The outcome was the same. So what does this mean? does the -v /var/tmp
in the docker run command map to the empty /var/tmp
dir exposed by the VOLUME
command in the Dockerfile and not the /var/tmp
directory with the test.txt
in the latest image?
Feeling a tad bit confused.
There is no image which contains
/var/tmp/test.txt
. The effect of declaring the volume before creating the file is that theRUN
instruction runs in a temporary container which has its own volume. Volumes bypass the Union File System so when the build saves that intermediate container, the contents of the volume are not saved, so they don't get persisted in the image layer.Every container you create from that image will have its own volume, the
-v
option doesn't change that, unless you use it to map the volume to a host path.Using your Dockerfile, you can see that by inspecting two containers. The first without the
-v
option:Here there are two volumes, mounted from
/var/lib/docker
on the host. One is from thenginx
base image, and one from your image. With the explicit-v
:Same outcome, but different host paths, because each container has its own volume.