BuildKit cache mounts: reusable scratch space
A small example
FROM busybox:1.37ARG REV=oneRUN --mount=type=cache,id=bm-v09-scratch,target=/scratch,sharing=locked \ if [ ! -f /scratch/marker ]; then printf "ready\n" > /scratch/marker; fi; \ cat /scratch/marker > /result.txt; printf "%s\n" "$REV" >> /result.txtCMD ["cat", "/result.txt"]The first execution creates a small marker if absent. Changing REV triggers a new result, while the mount can retain the marker from a previous run. sharing=locked coordinates concurrent writers to this cache. A cache may be removed by garbage collection or contain stale data, so correctness must never depend on its presence. In real builds cache package downloads, not trusted build outputs without validation. A build-time cache mount is not a runtime named volume. This fixture assumes a fresh dedicated cache ID containing only its own marker. A pre-existing arbitrary marker could change the output, so stop on an unexpected value. Neither locked sharing nor --no-cache validates or empties that mount.
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/06-cache-mount/ 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-mount:lab .docker run --rm --pull=never --network none bm-v09-cache-mount:labdocker build --load --network none --progress=plain --build-arg REV=two -t bm-v09-cache-mount:lab .docker run --rm --pull=never --network none bm-v09-cache-mount:labdocker run --rm --pull=never --network none bm-v09-cache-mount:lab sh -c "test ! -e /scratch/marker"Expect ready and one in the first run; ready and two in the next. The final check should succeed because the marker itself was not committed at /scratch/marker. These outputs do not prove that the mount was reused; inspect actual build evidence separately.
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 does not mean a cache mount is empty. Do not clear shared builder caches to perform a lesson. Use a new disposable lab VM for an empty-cache experiment.
Manual practice
Explain why this recipe works on a new builder. Design an additional check to confirm that a mounted file is not automatically present in the final image.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
BuildKit cache mounts: reusable scratch space — Knowledge check
Apply the build concept to a new scenario.
What if the mount is empty?
Does --no-cache necessarily empty a mount?
Which item is committed here?