I have the following result from docker build .
. Why doesn't Docker ever seem to cache step 4? Can I make it use cache with some tweak?
(The rest of the file as far as I know is irrelevant, but this failure to cache also results in steps 5 and following to build without cache.)
+ docker build .
Sending build context to Docker daemon 3.678MB
Step 1/36 : FROM mcr.microsoft.com/dotnet/core/sdk:3.1 as builder
---> c4155a9104a8
Step 2/36 : ARG branch_build_commit
---> Using cache
---> 2a8f43280a27
Step 3/36 : WORKDIR /tmp
---> Using cache
---> f3bf42ec98ab
Step 4/36 : RUN ln -snf /usr/share/zoneinfo/America/Chicago /etc/localtime && echo America/Chicago > /etc/timezone
---> Running in 32bd1e2cff9e
Removing intermediate container 32bd1e2cff9e
---> 8d49354c0de6
The Dockerfile documentation notes, under Impact [of ARG] on build caching:
...all
RUN
instructions following anARG
instruction use theARG
variable implicitly (as an environment variable), thus can cause a cache miss.
If this is in the context of a CI system and each build is normally passed a docker build --build-arg branch_build_commit=$(git rev-parse HEAD) .
or something similar, then every build will have a different ARG
value and RUN
instructions will always cause a cache miss.
On the other hand, it's merely convention that ARG
comes early in a Dockerfile so that it can be found. The Dockerfile documentation on Scope [of ARG] has some specific notes that ARG
values can only be used after the ARG
statement, and has an example of ARG
not being first. So you can make caching work again by moving the ARG
statement later in the file:
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 as builder
# These statements don't use the ARG value so they can come first.
WORKDIR /tmp
RUN ln -snf /usr/share/zoneinfo/America/Chicago /etc/localtime && echo America/Chicago > /etc/timezone
# Then you can declare the ARG value later
ARG branch_build_commit
# And any uses must come after the ARG statement.
RUN echo ${branch_build_commit:-unknown} > /etc/build-version
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With