Sub topic 11
Checking that the cache is used
This chapter assumes you can read a Dockerfile, have a working docker buildx builder with cache-from and cache-to set up, and know what a CI job log looks like. A pipeline can export and import cache without error and still rebuild every layer. You only find out by reading the build output.
Read the plain progress output
By default, buildx shows an interactive display that CI logs often collapse. Use --progress=plain to get one stable line per event.
docker buildx build --progress=plain \
--cache-from type=registry,ref=registry.example.com/myapp:buildcache \
-t myapp:test .Two things in the log matter:
- The import step. Near the top, BuildKit prints a step such as
importing cache manifest from registry.example.com/myapp:buildcache. If the reference does not exist yet, for example on the first run, this step reports an error and the build continues with a cold cache. - The
CACHEDmarker. A step restored from cache is followed byCACHEDand shows no run time.
#7 [2/5] COPY package.json package-lock.json ./
#7 CACHED
#8 [3/5] RUN npm ci
#8 CACHED
#9 [4/5] COPY . .
#9 DONE 0.4sThis is the result you want after a source-only change. The npm ci layer is restored, and only the layers from COPY . . onward run again.
Prove it with two runs
Run the same build twice with the same cache options. The second run should show CACHED on every step. If it does not, the cache is not working. Next, change one source file and run again. Only the layers after the earlier COPY of source files should rebuild.
In CI, also check that the job that exports cache actually ran. Pull requests often skip the push step, which leaves the cache stale.
Diagnose a miss
Work from the first uncached step downward. A layer is only reused if every layer before it was reused, so the first miss is the one to explain.
flowchart TDThe most common causes:
| Symptom | Likely cause | Fix |
|---|---|---|
| Import step errors | Wrong `ref`, `scope`, `bucket`, or `name`, or missing credentials | Match the values used in `cache-to` exactly |
| Everything rebuilds after `COPY . .` | `COPY . .` comes before the dependency install | Copy manifests first, install, then copy the source |
| Early layers always miss | A build argument or `.git` directory in the context changes each run | Pin the argument and list the directory in `.dockerignore` |
| Base image layers miss after a while | The `FROM` tag now points to a new digest | Pin a digest if you need stable results |
| Later stages of a multi-stage build miss | Cache exported with `mode=min` | Use `mode=max` |
| Hit on `main`, miss on a branch | Different scopes or no fallback | See the scoping chapter |
> The default docker driver cannot export these remote cache types. If cache-to fails with a driver error, create a docker-container builder.
Measure the effect
Compare total build time between a cold run and a warm run. Cache is worth keeping only if the warm run is clearly faster. If importing and exporting take longer than the steps they save, reconsider the backend or the cache mode.
Exercise
A build with --cache-from shows CACHED on the base image and npm ci steps on a rerun, but RUN npm run build still runs. What does that tell you, and what would you check first?
Show answer
The import works, since earlier layers were restored. The build step reruns because its inputs changed, which is expected after COPY . . brings in changed source. If the source did not change, check for files in the build context that change on every run, such as generated files or .git, and exclude them with .dockerignore.