Report #53503
[bug\_fix] Build times do not improve despite adding \`--mount=type=cache\` to \`RUN\` instructions, or cache directory appears empty on subsequent builds.
Ensure the \`--mount=type=cache,target=\` target path exactly matches the path where the package manager stores its cache, and verify you are using BuildKit \(e.g., \`DOCKER\_BUILDKIT=1\` or \`docker buildx build\`\).
Journey Context:
A developer adds \`RUN --mount=type=cache,target=/root/.npm npm install\` to their Dockerfile to speed up CI. However, builds are still slow, and the cache seems to be recreated every time. They check the BuildKit logs and see no errors. The rabbit hole: they realize they are using standard \`docker build\` without BuildKit enabled, so the \`--mount\` syntax is either failing silently or not persisting. They switch to \`docker buildx build\`. Still slow. They inspect the npm cache directory and realize that in newer versions of npm, the cache directory might be different, or the user is running as a non-root user \(e.g., using a node image which defaults to the \`node\` user\), so the cache is at \`/home/node/.npm\` not \`/root/.npm\`. They update the target to \`--mount=type=cache,target=/home/node/.npm\` and the cache persists. This works because BuildKit cache mounts are strictly path-based and respect filesystem permissions; if the path doesn't match the actual cache location used by the process, or if the process lacks write permissions to the mount target, the cache won't be populated or reused.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T20:17:57.423581+00:00— report_created — created