Learning center

The Docker build said CACHED, and the image kept last week's download

BuildKit matches a RUN instruction against its cache by the command text, so a RUN that downloads "latest" is not run again when the download changes. The rebuilt image keeps the old file and the build succeeds. Treat CACHED beside a network fetch as "this step did not run". Put the version in the instruction, verify a checksum, or rebuild that step without the cache before you trust what the image contains.

Why the instruction text is the key#

Docker's cache documentation says that the cache for RUN instructions "isn't invalidated automatically between builds". ADD, COPY and a RUN with a bind mount get a checksum of the files involved. For other RUN steps the builder does not examine what the command changed, and "just the command string itself is used to find a match". Build cache invalidation

A RUN curl, RUN wget, RUN git clone or RUN apt-get install with an unpinned version reads something the cache key does not include. The same holds for a RUN that fetches a branch tip.

Fixture: the server changed, the layer did not#

Docker 29.1.3 with buildx v0.39.1. A small HTTP server stands in for a vendor's "latest" download. The Dockerfile:

FROM alpine:3.22
RUN wget -qO /opt/tool.txt http://172.17.0.1:8765/latest.txt
CMD ["cat", "/opt/tool.txt"]

The server answers tool 1.8.0, the image is built, and then the server answers tool 1.9.0:

# upstream publishes a new "latest"
$ docker buildx build -t fixture-stale:dev .
[…]
#5 [2/2] RUN wget -qO /opt/tool.txt http://172.17.0.1:8765/latest.txt
#5 CACHED
[…]
$ docker run --rm fixture-stale:dev
tool 1.8.0
$ docker buildx build --no-cache -t fixture-stale:dev .
[…]
#5 [2/2] RUN wget -qO /opt/tool.txt http://172.17.0.1:8765/latest.txt
#5 DONE 0.4s
[…]
$ docker run --rm fixture-stale:dev
tool 1.9.0

The second build reported no error and produced an image with the version from the first. Only --no-cache ran the download again. In CI, a cache exported to a registry or to the GitHub Actions cache carries the same layer from one runner to the next. Cache storage backends

Choose a fix by what the step fetches#

The step fetchesFixWhat the build log then shows
A versioned releasePut the version in the instruction, for example through ARG TOOL_VERSION=1.9.0A changed command when the version changes
A file you can hashADD --checksum=sha256:… URL /destA failed build if the file changes
A Git repositoryADD --checksum=<commit> git-url or a commit in the commandThe commit you asked for
System packagesPinned versions, or a scheduled --no-cache rebuildFresh package versions on each rebuild

The Dockerfile reference documents --checksum for HTTP and Git sources; "For Git sources, the checksum is the commit SHA." The cache documentation notes that build arguments also invalidate the cache, which is why a version in an ARG works. --no-cache rebuilds every layer; --no-cache-filter STAGE rebuilds one stage.

--no-cache does not refresh the base image. Docker's build best practices say to add --pull for that, or to use both. The base image's own changes are covered in a base image tag that moved.

Read the log as evidence#

The plain progress log (--progress=plain) prints CACHED or DONE for each step. When you review an image for a release, find each step that reads the network and check which one it printed. CACHED there means the content came from an earlier build. The log cannot say when that build happened. The image itself can: run the tool's version command, or list the files the step wrote, and compare them with what the release notes say.

The same symptom, an old tool in a new image, also comes from a base image that already contains the tool, or a COPY of a vendored copy. docker history lists the instruction that created each layer.

For caches outside Docker, see a restore key that brought back old output.

One month of free code review.

New organizations get one calendar month of hosted code review for private and public GitHub repositories. No card or cloud connection required. Eligibility and fair-use terms.

Start free with GitHub

Related reading