Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is a call to "RUN ln" able to be cached by Docker?

Tags:

docker

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
like image 294
Patrick Szalapski Avatar asked Sep 15 '25 03:09

Patrick Szalapski


1 Answers

The Dockerfile documentation notes, under Impact [of ARG] on build caching:

...all RUN instructions following an ARG instruction use the ARG 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
like image 135
David Maze Avatar answered Sep 16 '25 22:09

David Maze