Report #45190
[bug\_fix] RUN --mount=type=cache does not persist cache across builds or throws cache key not found
Ensure the \`target\` path of the \`--mount=type=cache\` exactly matches the package manager's cache directory, and use the \`id\` parameter to share caches across different stages or builds.
Journey Context:
A developer switches to BuildKit and adds \`RUN --mount=type=cache,target=/root/.cache/pip pip install -r requirements.txt\` to speed up CI. However, the build time remains identical, and the cache seems empty. They check BuildKit logs and see the cache mount is created but pip isn't using it. The rabbit hole involves realizing that the base image \(e.g., a specific Python Alpine image\) configures pip to use a different cache directory \(like \`/home/user/.cache/pip\` or no cache at all\). The fix is to explicitly tell pip to use the mounted directory: \`RUN --mount=type=cache,target=/root/.cache/pip pip install --cache-dir /root/.cache/pip -r requirements.txt\`, ensuring the package manager and the mount target are perfectly aligned.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T06:19:21.661401+00:00— report_created — created