Docker Community Forums

Share and learn in the Docker community.

Files created not preserved in image

A file that is being created is not available in the next image layer.

Using the following file dockerfile

FROM jekyll/builder AS build-agent

COPY . .

RUN mkdir uml \
 && touch XXXX/img.png \
 && ls XXXX


and the following command

docker build -t guidance-dev --target=build-agent


Sending build context to Docker daemon  10.33MB
Step 1/5 : FROM jekyll/builder AS build-agent
 ---> a550a77c1ae9
Step 2/5 : WORKDIR /srv/jekyll
 ---> Using cache
 ---> 0d037ea607f5
Step 3/5 : COPY . .
 ---> 57f59b02006e
Step 4/5 : RUN mkdir uml  && touch XXXX/img.png  && ls XXXX
 ---> Running in eb04e6c32759
Removing intermediate container eb04e6c32759
 ---> e5d8b1fa2ee9
Step 5/5 : RUN ls XXXX
 ---> Running in 3525a5d81f45
Removing intermediate container 3525a5d81f45
 ---> 5d2995f98f6d
Successfully built 5d2995f98f6d
Successfully tagged guidance-dev:latest

As can be noticed in Step 4, file img.png exists but not in step 5.

The question is why this happens and how I can solve it.

I think it has to do with the way Jekyll is declaring the /srv/jekyll volume in their Dockerfile (

If I recreate your Dockerfile, and use any other directory than /srv/jekyll (say /tmp/), it works but trying to write something to the WORKDIR does not preserve it.

I suggest that you create a maker-script that build the files you need in the container before you build it. Something like this:

echo "#### Starting the script!"
mkdir uml
echo "#### Made the directory"
touch uml/img.png
echo "#### Touched the img"
echo "## This is the current path"
echo "## This is the current ls"
echo "## This is the current ls uml"
ls uml
docker build . 
rm -rf ./uml

Thanks @ovelindstrom, indeed the issue seems to be the volume. I change the output folder used inside the container and it is working as expected.