Sub topic 15
Exporting and Importing Build Cache in CI
This chapter assumes you know how the local build cache works, how a cache miss invalidates later steps, and how to write a Dockerfile with ordered instructions. It also assumes you have used docker build and have a CI system that runs builds.
Why CI loses the cache
The build cache lives in the builder's local storage. A fresh CI runner starts with an empty disk, so every step is a cache miss. Even a well-ordered Dockerfile rebuilds from the first instruction on every run.
The fix is to export the cache to a place that outlives the runner, then import it at the start of the next build. BuildKit does this with two flags on docker buildx build.
The two flags
--cache-fromtells BuildKit where to look for existing cache before it builds.--cache-totells BuildKit where to write cache after it builds.
docker buildx build \
--cache-from type=registry,ref=registry.example.com/app:buildcache \
--cache-to type=registry,ref=registry.example.com/app:buildcache,mode=max \
-t registry.example.com/app:latest \
--push .The registry backend stores the cache as a separate image reference in a registry. Here the reference is the tag buildcache. It is not a runnable image. It holds layers and metadata that BuildKit reads on the next build.
What `mode=max` changes
The mode option controls which layers are exported.
| Mode | What is exported | Trade-off |
|---|---|---|
| `min` (default) | Layers of the final image only | Small cache, but intermediate stages are rebuilt |
| `max` | Layers of all stages, including intermediate ones | Larger cache, better hit rate for multi-stage builds |
With a multi-stage build, the build stage is not part of the final image. In min mode its layers are not exported, so the compiler steps rebuild on every run. Use max for multi-stage Dockerfiles.
Choosing a backend
The registry backend is a common choice, but it is not the only one.
type=registryworks on any runner that can reach your registry.type=ghauses the GitHub Actions cache service.type=local,dest=pathwrites to a directory, which your CI can save and restore with its own cache feature.type=inlineembeds cache metadata in the pushed image. It supports onlyminmode.
docker buildx build \
--cache-from type=local,src=/tmp/buildx-cache \
--cache-to type=local,dest=/tmp/buildx-cache-new,mode=max \
-t app:ci .Writing to a new directory and then swapping it in avoids the local cache growing without limit, because the local backend does not remove old entries.
Common errors
- The default Docker driver rejects the flags. The classic
dockerdriver cannot export cache to a registry. Create a builder with thedocker-containerdriver first:docker buildx create --use. - No cache hits on the first run.
--cache-frompoints to a reference that does not exist yet. BuildKit continues without it, and the first run populates it. - Hits disappear after a small change. Cache matching follows the same rules as local builds. A changed
COPYinput invalidates that step and every later one, so ordering still matters. - Parallel builds overwrite each other. Two jobs writing to the same
refrace. Use a cache reference per branch, and import a fallback such as the main branch cache with a second--cache-from.
Exercise
Exercise
A multi-stage Dockerfile compiles code in a build stage and copies the binary into a small final stage. CI exports cache with --cache-to type=registry,ref=... and no mode option. The compile step reruns on every build even though the source is unchanged. What is the likely cause, and how do you fix it?
Show answer
The default mode is min, which exports only the layers of the final image. The build stage's layers are not in the cache, so the compile step is a miss each time. Add mode=max to --cache-to so the layers of all stages are exported.