Diagnosing Docker BuildKit cache misses
Find the first unexpected rebuild in a Docker build and trace it to an input change or an unavailable cache.
- First diagnostic
- Earliest cache miss
- File mtime alone
- Ignored for COPY checks
- Ephemeral runner
- Import an external cache
Which step should you investigate first?
The first unexpectedly rebuilt instruction in a Docker stage usually explains the later misses in its dependency chain. Inspect the build output from that instruction upward.
Compare the base image, instruction and inputs with the last build that reused the layer. Pin the same build context during the comparison so the experiment changes one input at a time.
What invalidates a COPY layer?
BuildKit checks file inputs for COPY and ADD; a change to file contents can invalidate their layer. A timestamp-only change to file modification time does not change that checksum.
Copy the dependency manifest and lock file before application sources when those inputs change at different rates. A source edit can then reuse a dependency-install layer when its actual inputs match.
Why is the cache warm locally and cold in CI?
A fresh CI builder cannot reuse a previous runner’s local cache unless the workflow imports a cache the builder can access. Exporting a cache after the build and importing it on the next run connects those executions.
Inspect the import and export lines in both job logs before editing the Dockerfile. Run the same commit twice on fresh workers to distinguish a missing cache from legitimate input changes.