Build cache keys and invalidation
A small example
FROM busybox:1.37WORKDIR /appCOPY message.txt .RUN tr a-z A-Z < message.txt > result.txtCMD ["cat", "/app/result.txt"]The COPY step depends on the selected file and its relevant metadata. A content change affects the operation and the uppercase conversion after it. Modification time alone is not part of the COPY cache checksum. For an ordinary RUN, the builder does not check whether a remote package repository changed since the previous build. In a simple linear stage, later dependent steps also lose reuse when the earlier result changes. Independent stages can behave differently. The mtime exception is specific to the cache checksum, not a promise that file timestamps in exported artifacts are identical. Record step outcomes before the next build overwrites the tag.
Try it in a local lab
Manual lab, not a ByteMind executable exercise. Use a new approved disposable Linux VM with blocked network egress and a local Engine and Buildx docker driver. Check docker context show, docker version, docker buildx version and docker buildx ls before doing anything. Confirm the reviewed, preloaded busybox:1.37 for the native platform, expected default user, no entrypoint, required applets and bundled frontend support for RUN mounts. Record its actual digest and versions. If a dependency needs to be fetched or the builder is remote, stop. --network none restricts RUN networking, not all builder traffic. Confirm all bm-v09 tags are unused. Run commands individually in the named fixture directory, stopping on any unexpected failure. All values are synthetic; do not use real credentials. Expected results are predictions, not observed output. Before querying or building, confirm DOCKER_HOST, DOCKER_CONTEXT and BUILDX_BUILDER overrides and the effective endpoint are approved; do not contact an unapproved endpoint. docker build normally uses the Engine default builder, not necessarily the starred Buildx builder. Record the effective local docker driver and platform; review the synthetic context and ignore rules. After each build, require exit status zero before running its tag; an earlier tag may still exist after failure.
Open lab-fixtures/docker-builds-and-caching/02-cache-keys/ in the extracted archive. The Dockerfile and these files are supplied. Work on a disposable copy, not the original archive.
docker build --load --network none --progress=plain -t bm-v09-cache-keys:lab .docker run --rm --pull=never --network none bm-v09-cache-keys:labdocker build --load --network none --progress=plain -t bm-v09-cache-keys:lab .printf "world\n" > message.txtdocker build --load --network none --progress=plain -t bm-v09-cache-keys:lab .docker run --rm --pull=never --network none bm-v09-cache-keys:labprintf "hello\n" > message.txtFirst run: HELLO. An unchanged second build can reuse cached work if the same builder still has it. After editing the file, expect WORLD and a new COPY/conversion result. Actual cache hits depend on the builder state.
Confirm the tag belongs to this lab and no containers depend on it. Remove only that tag without forcing. Cached data may remain; dispose of the VM rather than clearing a shared cache. Restore any edited fixture or discard the working copy.
Common mistake
--no-cache bypasses normal build result reuse, but does not by itself fetch a newer base image. --pull attempts to retrieve referenced images and is not allowed in this offline lab.
Manual practice
Predict which steps may be reused after changing only CMD. Then explain why identical inputs may still miss cache on a new builder.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Build cache keys and invalidation — Knowledge check
Apply the build concept to a new scenario.
Which edit changes the COPY input?
Does --no-cache guarantee a newer base image?
Why can an identical build miss cache?