Sub topic 7
Where cache mounts live and how to prune them
This chapter assumes you can write a Dockerfile, run docker build, and have already added a RUN --mount=type=cache instruction. It covers where BuildKit stores cache mount data, how long that data lasts, and how to inspect and delete it.
Where the cache lives
A cache mount is not part of the image. BuildKit keeps it in its own build cache, which is managed by the builder. With the default Docker builder, that storage sits inside the Docker daemon's data directory on the machine that runs the build. With a docker-container builder created by docker buildx create, it lives in a volume attached to the BuildKit container.
Three consequences follow:
- The cache is local to one builder on one machine. A colleague, or a CI runner with a different builder, starts with an empty cache.
- The cache never ships with the image. Pushing the image does not push the cache.
- The cache is keyed by its
id, which defaults to thetargetpath. TwoRUNsteps that mount the sameidshare one directory.
What keeps a cache alive
Cache mount contents survive between builds, even when the RUN instruction changes. Changing requirements.txt invalidates the layer, but the pip download cache is still there on the next run. This is the main benefit.
The cache is not permanent, though. It can disappear when:
- You run a prune command.
- BuildKit garbage collection removes it. The builder has a storage limit, and it deletes the least recently used entries when the limit is reached.
- You recreate or remove the builder.
- A CI runner is a fresh virtual machine, so the builder starts empty.
Treat a cache mount as an optimization only. A build must still produce a correct image when the cache is empty.
Inspecting build cache usage
Use docker buildx du to list build cache records, including cache mounts:
docker buildx du
docker buildx du --verboseThe output shows each record's ID, size, whether it is in use, and when it was last used. Cache mount records have the type exec.cachemount. The docker system df command shows a summary that includes a "Build Cache" row.
Pruning
Use docker buildx prune to delete build cache records for the current builder:
# Remove all cache records not currently in use
docker buildx prune
# Remove everything, including records marked as shared or recently used
docker buildx prune --all
# Keep 5 GB of cache and delete the rest
docker buildx prune --keep-storage 5GB
# Remove only records older than 7 days
docker buildx prune --filter until=168h
# Remove only cache mount records
docker buildx prune --filter type=exec.cachemountdocker builder prune accepts the same flags for the default builder. Pruning a cache mount means the next build downloads or recompiles everything once, then refills the cache.
You can also set a storage limit on the builder so that garbage collection runs without you. For a docker-container builder, pass a BuildKit configuration file with --buildkitd-config. For the default builder, set the limits in the daemon configuration under builder.gc.
Common mistakes
- Expecting a cache mount to speed up a fresh CI runner. The cache is empty there. Use the CI system's cache feature or the
--cache-fromand--cache-tooptions with a cache backend, and remember that these mainly cover layer cache, not every cache mount. - Relying on files in the cache directory being present. Another prune or a garbage collection run can remove them.
- Putting output you need in the image into a cache mount target. The mount is not in the final layer, so copy the file out in the same
RUNstep.
Exercise
You added RUN --mount=type=cache,target=/root/.npm npm ci. After a teammate pulls your Dockerfile and builds on their laptop, the first build is slow. Why, and which command shows how much cache their builder holds?
Show answer
Cache mounts live in the local builder's storage and are not part of the image or pushed with it, so their builder starts with an empty cache. They can run docker buildx du to see the cache records and their sizes.
Exercise
Write a command that deletes cache mount records older than 48 hours and leaves other build cache untouched.
Show answer
docker buildx prune --filter type=exec.cachemount --filter until=48h