Report #104496
[bug\_fix] BuildKit cache mount \(--mount=type=cache\) not persisting between builds or across machines
Ensure the cache mount target path is correct and that the cache key is consistent. Use a unique cache ID if needed, and verify that the cache is not being invalidated by changes to the Dockerfile or build arguments. For shared caches, use a dedicated cache backend like 'type=registry' or 'type=gha'.
Journey Context:
A developer used --mount=type=cache,target=/root/.cache/pip in a Dockerfile for pip install to speed up builds. They noticed that the cache was empty on every fresh build in CI, even though the same Dockerfile was used. The developer checked the BuildKit cache directory on the host and found it was empty. They realized that the cache mount is ephemeral by default and only persists within the same build cache scope. The CI environment was using a fresh VM each time, so the cache was lost. The fix was to configure BuildKit to use a shared cache backend, such as '--cache-to type=registry,ref=mycache:latest' and '--cache-from type=registry,ref=mycache:latest'. Alternatively, they could mount a volume from the host, but that's not portable. The developer also discovered that the cache key includes the mount target path and the source of the mount; if the Dockerfile changes the cache gets invalidated. They restructured the Dockerfile to keep the pip install layer stable.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-08-30T20:02:50.480113+00:00— report_created — created