Change in env variable value affects layer cache of all the instructions that comes after ENV
.
$ cat Dockerfile
FROM alpine
ARG COMMIT_SHA
ENV COMMIT_SHA ${COMMIT_SHA}
RUN apk add --no-cache curl
CMD ["/bin/sh", "-c", "echo $COMMIT_SHA"]
$ docker build --build-arg COMMIT_SHA=123 -t test .
Sending build context to Docker daemon 2.56kB
Step 1/5 : FROM alpine
---> d7d3d98c851f
Step 2/5 : ARG COMMIT_SHA
---> Using cache
---> c1d9f2578131
Step 3/5 : ENV COMMIT_SHA ${COMMIT_SHA}
---> Running in 80a95e57584b
Removing intermediate container 80a95e57584b
---> a60a040ccbee
Step 4/5 : RUN apk add --no-cache curl
---> Running in 39926537efca
fetch https://dl-cdn.alpinelinux.org/alpine/v3.16/main/x86_64/APKINDEX.tar.gz
fetch https://dl-cdn.alpinelinux.org/alpine/v3.16/community/x86_64/APKINDEX.tar.gz
(1/5) Installing ca-certificates (20211220-r0)
(2/5) Installing brotli-libs (1.0.9-r6)
(3/5) Installing nghttp2-libs (1.47.0-r0)
(4/5) Installing libcurl (7.83.1-r2)
(5/5) Installing curl (7.83.1-r2)
Executing busybox-1.35.0-r15.trigger
Executing ca-certificates-20211220-r0.trigger
OK: 8 MiB in 19 packages
Removing intermediate container 39926537efca
---> aab21149f4c6
Step 5/5 : CMD ["/bin/sh", "-c", "echo $COMMIT_SHA"]
---> Running in dd75ef5a7809
Removing intermediate container dd75ef5a7809
---> 4cbe3d3043cf
Successfully built 4cbe3d3043cf
Successfully tagged test:latest
$ docker build --build-arg COMMIT_SHA=123 -t test .
Sending build context to Docker daemon 2.56kB
Step 1/5 : FROM alpine
---> d7d3d98c851f
Step 2/5 : ARG COMMIT_SHA
---> Using cache
---> c1d9f2578131
Step 3/5 : ENV COMMIT_SHA ${COMMIT_SHA}
---> Using cache
---> a60a040ccbee
Step 4/5 : RUN apk add --no-cache curl
---> Using cache
---> aab21149f4c6
Step 5/5 : CMD ["/bin/sh", "-c", "echo $COMMIT_SHA"]
---> Using cache
---> 4cbe3d3043cf
Successfully built 4cbe3d3043cf
Successfully tagged test:latest
$ docker build --build-arg COMMIT_SHA=234 -t test .
Sending build context to Docker daemon 2.56kB
Step 1/5 : FROM alpine
---> d7d3d98c851f
Step 2/5 : ARG COMMIT_SHA
---> Using cache
---> c1d9f2578131
Step 3/5 : ENV COMMIT_SHA ${COMMIT_SHA}
---> Running in 2931623c86da
Removing intermediate container 2931623c86da
---> 0abda51c94ad
Step 4/5 : RUN apk add --no-cache curl
---> Running in b214e18f44ca
fetch https://dl-cdn.alpinelinux.org/alpine/v3.16/main/x86_64/APKINDEX.tar.gz
fetch https://dl-cdn.alpinelinux.org/alpine/v3.16/community/x86_64/APKINDEX.tar.gz
(1/5) Installing ca-certificates (20211220-r0)
(2/5) Installing brotli-libs (1.0.9-r6)
(3/5) Installing nghttp2-libs (1.47.0-r0)
(4/5) Installing libcurl (7.83.1-r2)
(5/5) Installing curl (7.83.1-r2)
Executing busybox-1.35.0-r15.trigger
Executing ca-certificates-20211220-r0.trigger
OK: 8 MiB in 19 packages
Removing intermediate container b214e18f44ca
---> b510272c64e7
Step 5/5 : CMD ["/bin/sh", "-c", "echo $COMMIT_SHA"]
---> Running in 82684e335cfc
Removing intermediate container 82684e335cfc
---> 28259be9ca05
Successfully built 28259be9ca05
Successfully tagged test:latest