Sub topic 6
The registry backend
This chapter assumes you can write a Dockerfile, you know what an image tag is, and you have a docker-container builder available through buildx. The default docker driver cannot export cache to a registry, which the builder chapter covers.
How the registry backend works
The registry backend stores BuildKit cache as an image-like artifact in any OCI-compatible registry. You choose a tag for it, such as myapp:buildcache. At the end of a build, BuildKit pushes the cache layers and a cache manifest to that tag. On the next build, possibly on a different runner, BuildKit reads the manifest and pulls only the layers it needs.
The cache tag is separate from your application image tag. Keep them separate so a cache push never overwrites a release image.
A working build
Create a builder, then pass the same reference to --cache-from and --cache-to.
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 .You must be logged in to the registry (docker login) before the build, because the cache push needs write access.
The options
| Option | Used in | Meaning |
|---|---|---|
| `type=registry` | both | Selects the registry backend. |
| `ref` | both | Full image reference of the cache tag. |
| `mode` | `cache-to` | `min` exports only layers of the final image. `max` exports all layers of all stages. |
| `image-manifest=true` | `cache-to` | Writes the cache as an image manifest instead of an index. Some registries need this. |
| `oci-mediatypes=true` | `cache-to` | Uses OCI media types. Needed together with `image-manifest` on strict registries. |
Use mode=max for multi-stage Dockerfiles. With min, intermediate stages such as a build stage are not exported, so the expensive npm ci and npm run build steps miss on a cold runner.
Making the cache hit
The cache only helps if the Dockerfile keeps early layers stable. Copy manifests and install dependencies before copying source.
FROM node:20-slim
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci
COPY . .
RUN npm run build
CMD ["node", "dist/index.js"]Changing a source file invalidates the COPY . . layer and later ones. The npm ci layer stays cached until a manifest changes.
Checking the import
Run with plain progress output to see which steps came from the cache.
docker buildx build --progress=plain \
--cache-from type=registry,ref=registry.example.com/myapp:buildcache \
-t myapp:test .Steps restored from the remote cache print CACHED. If every step runs, the import failed or the cache keys did not match.
Common errors
- Registry rejects the cache push. Some registries do not accept the default cache index. Add
image-manifest=true,oci-mediatypes=truetocache-to. - Nothing is cached on the first run. This is expected. The first build has no cache to import, and it only creates one. A missing cache tag does not fail the build, so a typo in
reffails silently as a miss. - Unauthorized on push. Pull-only credentials work for
cache-frombut notcache-to. Give the CI job push rights, or only export cache on trusted branches. - The cache tag grows over time. Each push replaces the tag, but old blobs may stay in the registry until garbage collection. Configure registry cleanup.
Exercise
A multi-stage build is fast on your laptop but slow on every CI run, even though you use --cache-from and --cache-to type=registry,ref=...,mode=min. The slow steps are in the build stage. What change should you try first, and why?
Show answer
Change mode=min to mode=max. With min, BuildKit exports only the layers of the final image, so the intermediate build stage (with npm ci and npm run build) is not stored. A cold runner then rebuilds those steps. With max, all stages are exported and can be restored as CACHED.