Report #53120
[bug\_fix] RUN --mount=type=cache does not persist cache across builds
Ensure the CI pipeline or build command is not using the \`--no-cache\` flag, which wipes out the BuildKit cache state. Additionally, verify that the \`target\` path exactly matches the directory where the package manager actually writes its cache \(e.g., \`/root/.cache/pip\` for pip, \`/usr/local/share/.cache/yarn\` for Yarn\), and assign a consistent \`id\` to the mount if sharing across steps.
Journey Context:
A developer migrates their Dockerfile to BuildKit syntax to speed up CI by caching package dependencies. They add \`\# syntax=docker/dockerfile:1\` and change their \`RUN pip install\` to \`RUN --mount=type=cache,target=/root/.cache/pip pip install -r requirements.txt\`. The build succeeds, but CI times remain unchanged; dependencies are re-downloaded every time. The developer initially suspects a permissions issue or a BuildKit bug, spending hours tweaking the \`id\` parameter and trying different target paths. They eventually inspect the CI runner's execution script and discover the pipeline is running \`docker build --no-cache .\` to prevent stale Docker layer caches. The rabbit hole reveals that \`--no-cache\` instructs BuildKit to discard all intermediate snapshots, including the explicitly defined cache mounts. The fix works because removing \`--no-cache\` allows BuildKit to persist the cache mount snapshots between runs, while correctly identifying the package manager's true cache directory ensures the tool actually reads and writes to the mounted volume.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T19:39:25.773079+00:00— report_created — created