Skip to content

Instantly share code, notes, and snippets.

@nilesh-akhade
Created July 25, 2022 09:56
Show Gist options
  • Save nilesh-akhade/33575305b1a990c58974cb59640b2f70 to your computer and use it in GitHub Desktop.
Save nilesh-akhade/33575305b1a990c58974cb59640b2f70 to your computer and use it in GitHub Desktop.
docker build layer cache

Change in COMMIT_SHA invalidates cache

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

Change in COMMIT_SHA does not affect docker layer cache

The apk add instruction comes before ENV instruction hence docker layer cache is used.

$ cat Dockerfile
FROM alpine
RUN apk add --no-cache curl
CMD ["/bin/sh", "-c", "echo $COMMIT_SHA"]
ARG COMMIT_SHA
ENV COMMIT_SHA ${COMMIT_SHA}

$ 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 : RUN apk add --no-cache curl
 ---> Using cache
 ---> df1852ccfaab
Step 3/5 : CMD ["/bin/sh", "-c", "echo $COMMIT_SHA"]
 ---> Using cache
 ---> 32af8fbb695e
Step 4/5 : ARG COMMIT_SHA
 ---> Using cache
 ---> baa084649908
Step 5/5 : ENV COMMIT_SHA ${COMMIT_SHA}
 ---> Using cache
 ---> 3b5730a7cabf
Successfully built 3b5730a7cabf
Successfully tagged test:latest

$ docker run test
234

$ docker build --build-arg COMMIT_SHA=173 -t test .
Sending build context to Docker daemon   2.56kB
Step 1/5 : FROM alpine
 ---> d7d3d98c851f
Step 2/5 : RUN apk add --no-cache curl
 ---> Using cache
 ---> df1852ccfaab
Step 3/5 : CMD ["/bin/sh", "-c", "echo $COMMIT_SHA"]
 ---> Using cache
 ---> 32af8fbb695e
Step 4/5 : ARG COMMIT_SHA
 ---> Using cache
 ---> baa084649908
Step 5/5 : ENV COMMIT_SHA ${COMMIT_SHA}
 ---> Running in 0befb0796096
Removing intermediate container 0befb0796096
 ---> b1fd2d01a10f
Successfully built b1fd2d01a10f
Successfully tagged test:latest

$ docker run test
173
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment