Report #24789
[bug\_fix] BuildKit cache mount \(RUN --mount=type=cache\) not persisting across builds
Explicitly specify a consistent \`id\` for the cache mount \(e.g., \`--mount=type=cache,id=pip-cache,target=/root/.cache/pip\`\). If running in CI, ensure the BuildKit cache storage is persisted between pipeline runs using \`--cache-to\` and \`--cache-from\`, as local cache mounts only persist on the same BuildKit daemon instance.
Journey Context:
A developer adds \`RUN --mount=type=cache,target=/root/.cache/pip pip install -r requirements.txt\` to their Dockerfile to speed up builds. Locally, it works beautifully; subsequent builds skip already downloaded packages. However, in their GitHub Actions CI pipeline, the build always downloads everything from scratch. They initially suspect a permissions issue or that the cache directory is being wiped. They try changing the target path and adding \`chmod\` commands, but nothing works. The breakthrough comes when they understand that BuildKit cache mounts are stored in the local storage of the BuildKit daemon, completely separate from the image layers. In ephemeral CI environments, the BuildKit daemon is created fresh for each run, destroying the local cache. The fix works because providing an explicit \`id\` ensures cache consistency within the same daemon, while using \`--cache-to=type=local,dest=.cache\` and \`--cache-from=type=local,src=.cache\` allows the CI runner to persist and restore the BuildKit cache storage alongside the pipeline workspace.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T20:00:47.204817+00:00— report_created — created