In this post, we look into a perplexing issue that happens often when we try to extend an offical image.
In summary, there is currently no way to add additional content into VOLUME
directory in a trivial way.
If you unknowingly adds files into a folder that has been used as a VOLUME
mount point in a Docker image or one of its base images, the files cannot be found for seemingly no reason.
Problem description
Let’s say we created the following Dockerfile for container, extending on top of a base Docker image:
1 2 3 4 5 6 7 8 9 10 |
|
When we try to build the above image with docker build
, we should expect several files dropped into the home /home/jenkins
folder.
In fact, we can add several echo
commands into the above Dockerfile to verify in the build log that the files are actually created.
However, when we start running a container from this new Docker image using docker run
, the files simply don’t exist in /home/jenkins
.
This was very perplexing at first.
Struggling with different options of rebuilding (docker build
) and re-running (docker run
) gives no different outcomes.
It eventually turns out that /home/jenkins
is mounted as a volume in the base
image or one of its base images.
If you have access to the base images' Dockerfiles, you should expect to find the following lines in one of the base Dockerfiles:
1 2 3 4 |
|
Otherwise, you can verify with the following docker inspect
command when the Docker container is still running:
1 2 |
|
This problem is already seen and reported in this issue.
There have been suggestions that VOLUME
directive in Dockerfile is a mistake.
It should be an option/directive when running docker run
, not when building images.
Resolving problem
The above problem can be resolved by simply adding files into another path that has NOT been used as VOLUME.
If the specific VOLUME
ed path (/home/path
in the example) must be used, you can also use docker copy
to add files into a running container (see last post).