Sub topic 5
What gets exported: mode=min versus mode=max
This chapter assumes you can write a multi-stage Dockerfile, know what a layer is, and have seen the --cache-to and --cache-from flags in docker buildx build. You should also have a docker-container builder, because the default Docker driver cannot export cache to a remote backend.
The question mode answers
When BuildKit exports cache, it must decide which build steps to write to the backend. The mode option on --cache-to controls that choice. It applies to the registry, gha, and s3 backends alike.
mode=minexports cache only for layers that end up in the final image. This is the default.mode=maxexports cache for every layer of every stage, including intermediate stages that are discarded from the final image.
The difference matters most for multi-stage builds. A typical build stage installs dev dependencies and compiles code, then a runtime stage copies only the output. With mode=min, the expensive build stage is not cached, because none of its layers are in the final image.
A worked example
Consider this multi-stage Dockerfile.
FROM node:20-slim AS build
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci
COPY . .
RUN npm run build
FROM node:20-slim
WORKDIR /app
COPY --from=build /app/dist ./dist
CMD ["node", "dist/index.js"]The final image contains only the base image, WORKDIR, and the copied dist directory. The npm ci and npm run build steps live in the build stage.
With mode=min, a fresh runner imports the cache and finds nothing for npm ci. It reinstalls every dependency, even when package-lock.json has not changed. With mode=max, those steps are in the cache and show as CACHED.
Here is the export command with mode=max set:
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 \
-t registry.example.com/myapp:latest \
--push .Trade-offs
| Aspect | mode=min | mode=max |
|---|---|---|
| Layers exported | Final-image layers only | All layers from all stages |
| Cache size | Smaller | Larger |
| Upload time | Shorter | Longer |
| Hit rate on multi-stage builds | Low for build stages | High |
| Difference for single-stage builds | Little or none | Little or none |
For a single-stage Dockerfile, every layer is in the final image, so the two modes export nearly the same content. Choose max when you have multi-stage builds, which is the common case in CI.
The cost of max is storage and transfer. Build stages often hold node_modules, compiler caches, or downloaded toolchains, which can be much larger than the runtime image. This matters on backends with a size cap. The GitHub Actions cache has a per-repository limit, so a large max export can evict older entries sooner. Later chapters cover retention in detail.
Common mistakes
- Leaving
modeunset on a multi-stage build and wondering why the build stage always runs from scratch. - Setting
mode=maxon--cache-from. The mode option belongs only on--cache-to, since importing reads whatever is there. - Expecting
mode=maxto work on the defaultdockerdriver. Export requires thedocker-containerdriver.
Exercise
A team's CI uses a two-stage Dockerfile and --cache-to type=gha,scope=myapp. Every run reinstalls npm dependencies even though package-lock.json never changes. What is the likely cause, and what is the fix?
Show answer
The default mode=min exports only layers in the final image, and the npm ci step is in the discarded build stage, so it is never cached. Add mode=max to the cache-to option: --cache-to type=gha,scope=myapp,mode=max.