1

Why the cache layer of the RUN command is invalidate?

FROM python:3.7-slim
ARG VERSION
RUN pip install --upgrade pip && pip install -r requirements.txt
ENV VERSION_2 2
ENV VERSION $VERSION
CMD ["env"]

As shown in the dockerfile above, using build arg to dynamically set environment variables will cause the cache layer of the RUN instruction to become invalid, because build-arg will change every time it is built, and the history of the image is as follows

IMAGE          CREATED         CREATED BY                                      SIZE      COMMENT
13d8c1c91aed   2 minutes ago   CMD ["env"]                                     0B        buildkit.dockerfile.v0
<missing>      2 minutes ago   ENV VERSION=1                                   0B        buildkit.dockerfile.v0
<missing>      2 minutes ago   ENV VERSION_2=2                                 0B        buildkit.dockerfile.v0
<missing>      2 minutes ago   RUN |1 VERSION=1 /bin/sh -c pip install --up…   5.28MB    buildkit.dockerfile.v0
<missing>      2 minutes ago   ARG VERSION                                     0B        buildkit.dockerfile.v0
<missing>      7 days ago      /bin/sh -c #(nop)  CMD ["python3"]              0B        
<missing>      7 days ago      /bin/sh -c set -ex;   savedAptMark="$(apt-ma…   8.49MB    
<missing>      7 days ago      /bin/sh -c #(nop)  ENV PYTHON_GET_PIP_SHA256…   0B        
<missing>      7 days ago      /bin/sh -c #(nop)  ENV PYTHON_GET_PIP_URL=ht…   0B        
<missing>      7 days ago      /bin/sh -c #(nop)  ENV PYTHON_PIP_VERSION=20…   0B        
<missing>      7 days ago      /bin/sh -c cd /usr/local/bin  && ln -s idle3…   32B       
<missing>      7 days ago      /bin/sh -c set -ex   && savedAptMark="$(apt-…   27.1MB    
<missing>      7 days ago      /bin/sh -c #(nop)  ENV PYTHON_VERSION=3.7.9     0B        
<missing>      7 days ago      /bin/sh -c #(nop)  ENV GPG_KEY=0D96DF4D4110E…   0B        
<missing>      7 days ago      /bin/sh -c set -eux;  apt-get update;  apt-g…   7.03MB    
<missing>      8 days ago      /bin/sh -c #(nop)  ENV LANG=C.UTF-8             0B        
<missing>      8 days ago      /bin/sh -c #(nop)  ENV PATH=/usr/local/bin:/…   0B        
<missing>      8 days ago      /bin/sh -c #(nop)  CMD ["bash"]                 0B        
<missing>      8 days ago      /bin/sh -c #(nop) ADD file:422aca8901ae3d869…   69.2MB
leosocy
  • 31
  • 3
  • The `ARG` values are visible to the `RUN` command as environment variables, so if the `ARG` changes, the `RUN` command needs to be re-run too. – David Maze Jan 20 '21 at 16:44

1 Answers1

2

The docker docs has a clear explanation, well, I was careless.

https://docs.docker.com/engine/reference/builder/#impact-on-build-caching

leosocy
  • 31
  • 3