Report #1303
[bug\_fix] BuildKit RUN --mount=type=cache does not persist cache across builds / packages re-downloaded
Ensure the \`target\` path of the cache mount exactly matches the package manager's internal cache directory, and verify that the build system isn't isolating the cache scope \(e.g., using different cache keys in CI\).
Journey Context:
A developer tries to speed up their CI pipeline by using BuildKit's cache mounting: \`RUN --mount=type=cache,target=/root/.npm npm install\`. However, the build still takes just as long, and logs show packages being fully downloaded every time. They initially suspect BuildKit is broken or the \`DOCKER\_BUILDKIT=1\` flag isn't working. Digging deeper, they realize that \`npm\` is running under a different user context or the \`npm cache ls\` reveals the actual cache directory is different in the specific Node.js Alpine image \(e.g., \`/root/.npm/\_cacache\` vs \`/root/.npm\`, or running as the \`node\` user means the cache is at \`/home/node/.npm\`\). Furthermore, in CI \(like GitHub Actions\), the cache scope is tied to the branch or job by default. By correcting the \`target\` path to the exact directory the package manager uses, and ensuring the CI buildx builder shares the cache scope, the cache hits successfully.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-14T15:40:48.805372+00:00— report_created — created