Dzhuneyt
Dzhuneyt

Reputation: 8701

Dockerfile - removing a file in one RUN command, it is still present in the next RUN command

I have a Dockerfile (https://gist.github.com/hasMobi/e198555704ee57e84399) that have these two commands in sequence:

RUN rm -frv /usr/share/nginx/html/*
RUN ls /usr/share/nginx/html/

When I look at the console while building hte image, I can clearly see 2 files being removed from that folder, but when the next RUN command comes, it lists the directory's contents and the files are still there?:

Step 6 : RUN rm -fry /usr/share/nginx/html/* 
 ---> Running in b9a69992e4e0 
removed '/usr/share/nginx/html/index.html' 
removed '/usr/share/nginx/html/index.php' 
 ---> 2bfe01cbd007 
Removing intermediate container b9a69992e4e0 
Step 7 : RUN is /usr/share/nginx/html/ 
 ---> Running in 08396f6029e1 
index.html 
index.php 
 ---> a6471052519d 

What is going on here? I understand that each RUN command creates a separate layer and one is isolated from the other, but isn't the second RUN command supposed to inherit the file system in the exact state that it was in the previous RUN command (with the 2 files gone)?

Upvotes: 36

Views: 160619

Answers (3)

mr.wolle
mr.wolle

Reputation: 1326

Either:

  • the files your are trying to remove are within a volume (as noted in the comments) or
  • you are using the legacy builder (which seems to be the default on Ubuntu jammy)

DEPRECATED: The legacy builder is deprecated and will be removed in a future release. Install the buildx component to build images with BuildKit: https://docs.docker.com/go/buildx/

docker buildx build instead of docker build fixed the issue for me.

Install the docker plugin with sudo apt install docker-buildx-plugin.

Upvotes: 0

PaulC
PaulC

Reputation: 555

I had a similar issue:

   RUN rm -rf /full/path
   RUN ln -s /other /full/path

This fails because "/full/path" still exists on the second RUN. This workaround works:

   RUN rm -rf /full/path; ln -s /other /full/path

I don't understand the behavior but was able to work around it in my case.

Upvotes: 21

ntwrkguru
ntwrkguru

Reputation: 1754

Basically, the ADD commands from the base image are overwriting the RUN commands in your Dockerfile. See this for more information.

Note: The first encountered ADD instruction will invalidate the cache for all following instructions from the Dockerfile if the contents of have changed. This includes invalidating the cache for RUN instructions. See the Dockerfile Best Practices guide for more information.

You may consider forking the source base image and using your customized version instead.

Upvotes: 4

Related Questions