Report #11873
[bug\_fix] BuildKit RUN --mount=type=cache does not persist between builds; cache appears empty or is rebuilt every time
Specify an explicit id= parameter in the cache mount directive \(e.g., RUN --mount=type=cache,id=pip-cache,target=/root/.cache/pip\). In CI environments, also use docker buildx with --cache-to and --cache-from pointing to a registry to persist the BuildKit cache across runs, since CI runners typically start with a fresh daemon.
Journey Context:
A developer adds RUN --mount=type=cache,target=/root/.cache/pip pip install -r requirements.txt to their Dockerfile, expecting pip packages to be cached between builds. Locally it seems to work sometimes but not always. In CI \(GitHub Actions\), the cache is always empty — every build reinstalls all packages. They initially blame pip, then suspect the cache mount syntax. The real issue is twofold: \(1\) Without an explicit id, BuildKit uses the target path as the cache key, which can collide or get invalidated unexpectedly when the target path changes or multiple mounts share similar paths. \(2\) In CI, the BuildKit daemon's local cache storage is ephemeral — each CI run gets a fresh Docker daemon with no persisted cache state. The fix is to assign a stable, unique id to the cache mount, and in CI, export/import the cache to a registry using --cache-to=type=registry,ref=myimage:cache and --cache-from=type=registry,ref=myimage:cache. This ensures the cache survives across CI runs.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T14:26:23.106392+00:00— report_created — created