Sub topic 3
Exporting and importing cache with buildx
This chapter assumes you can write a Dockerfile, run docker build, and push images to a registry. It also assumes you know why a fresh CI runner has an empty layer cache. Here you learn the two buildx flags that move cache between builds.
The two flags
BuildKit is the build engine behind docker buildx build. It can save its cache to a remote location and load it back later. Two flags control this:
--cache-tois the exporter. After the build finishes, BuildKit writes cache data to the location you name.--cache-fromis the importer. Before the build starts, BuildKit reads cache data from the location you name and uses it to skip steps.
Both flags take a comma-separated list of key=value pairs. The first pair, type=, picks the backend. The other pairs depend on the backend. Later chapters cover registry, gha, and s3 in detail.
A first example
This command builds an image, pushes it, and also pushes cache to a separate tag in the same registry:
docker buildx create --name ci-builder --driver docker-container --use
docker buildx build \
--cache-from type=registry,ref=registry.example.com/myapp:buildcache \
--cache-to type=registry,ref=registry.example.com/myapp:buildcache,mode=max,image-manifest=true,oci-mediatypes=true \
-t registry.example.com/myapp:latest \
--push .The first line creates a builder that supports remote cache export. The default Docker builder does not, so the next chapter explains why. The ref value names where the cache lives. Here it is a tag called buildcache, which keeps cache data apart from your real image tags. The mode=max option exports every stage, and a later chapter explains it.
What happens during a build
BuildKit matches steps by cache key. The key is computed from the instruction and its inputs, such as the parent layer and the files a COPY reads. If the key matches an imported record, BuildKit pulls that step's result and does not run it. If the key does not match, BuildKit runs the step normally. A miss is not an error, so a build with an empty or missing cache simply runs cold.
Other exporter types
The registry, gha, and s3 backends are the focus of this tutorial. Two others exist, and you will see them in older guides:
| Type | Where cache goes | Note |
|---|---|---|
| `inline` | Embedded in the pushed image | Only exports the final image's layers, like `mode=min` |
| `local` | A directory on disk | Useful when a CI system can save a directory between runs |
| `registry` | A separate image reference | Supports `mode=max` |
Use a separate cache reference when you need intermediate stages from a multi-stage build to be cached.
Reading the options
typeselects the backend and is required.modeisminormax. It applies to--cache-toonly. The default ismin.ref(registry),scope(gha), andname(s3) identify one cache among many. Use the same value in--cache-fromand--cache-to, or the importer will not find what the exporter wrote.
You can pass --cache-from more than once. BuildKit then checks each source in turn. This is how you add a fallback, such as a main-branch cache, which a later chapter covers.
Common errors
- "Cache export is not supported for the docker driver." You are using the default builder. Create a
docker-containerbuilder and select it with--use. - No cache hits on the first run. This is expected. The first build has nothing to import, and it only exports.
- Cache never hits afterward. Check that the import and export values match exactly, including the tag, scope, or name.
Exercise
A build uses --cache-to type=registry,ref=registry.example.com/myapp:buildcache but no --cache-from. The next CI run is slow again. Why, and how do you fix it?
Show answer
The exporter only writes cache. Without --cache-from, BuildKit never reads it, so each run starts cold. Add --cache-from type=registry,ref=registry.example.com/myapp:buildcache with the same ref.