Report #93745
[bug\_fix] BuildKit RUN --mount=type=cache does not persist across builds; packages re-download every time
Specify an explicit \`id\` parameter on the cache mount \(e.g., \`--mount=type=cache,id=pip-cache,target=/root/.cache/pip\`\) to create a stable cache key independent of the target path. Ensure BuildKit is enabled and that \`docker builder prune\` is not being run between builds. In CI, configure the builder to use a persistent cache backend.
Journey Context:
A developer adds \`RUN --mount=type=cache,target=/root/.cache/pip pip install -r requirements.txt\` to their Dockerfile to cache pip downloads. The first build works, but subsequent builds still re-download all packages. They discover that without an explicit \`id\`, BuildKit uses the \`target\` path as the cache key. If the target path changes slightly between builds or if the Dockerfile is refactored, the cache key changes and the cache is lost. They also discover that their CI pipeline runs \`docker builder prune\` as a cleanup step, which wipes the BuildKit cache entirely. The fix is to add an explicit \`id=pip-cache\` for stability and to remove or scope the \`docker builder prune\` step. In CI environments like GitHub Actions, they may also need to configure \`docker buildx\` with a persistent cache backend \(e.g., \`--cache-to=type=gha\`\) to survive runner recycling.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T15:56:11.447132+00:00— report_created — created