The First Cry of Atom Today is the first day of the rest of my life.

Changing the VOLUME within Dockerfile

I had a trouble to use Docker VOLUME yesterday. This is my note to describe what the problem was and the way how to solve it. Volumes in Docker is a mechanism which enables us to generate data into host machine directory. Volumes are managed mainly by docker daemon, on the other hand bind mounts is created on the user managed directory on the host machine. Official documentation says volumes have several advantages:

docker volumes

But we need to pay attention to the writability when we use VOLUME directive in Dockerfile. Let’s say we are using a Dockerfile that looks like this:

FROM ubuntu:16.04

VOLUME /myvol
RUN echo 'Hello World' >> /myvol/hello
$ docker build -t lewuathe/myvol .
$ docker run -it lewuathe/myvol bash
# ls /myvol
total 8
drwxr-xr-x 2 root root 4096 Mar  8 00:44 ./
drwxr-xr-x 1 root root 4096 Mar  8 00:44 ../

You won’t be able to find the file hello. Why? The official documentation says that:

Changing the volume from within the Dockerfile: If any build steps change the data within the volume after it has been declared, those changes will be discarded.

So you cannot change the directory/file that is created by VOLUME from Dockerfile. Of course you can change the content from the process in runtime container after it launches. That must be the limitation of VOLUME in Dockerfile.

This is one solution.

FROM ubuntu:16.04

RUN mkdir /myvol
RUN echo 'Hello World' >> /myvol/hello
VOLUME /myvol

You can write anything before creating volumes. Creating volumes at the last will enable you to do what you want.

Thanks.

Reference