Report #43756
[bug\_fix] RUN --mount=type=cache,target=/root/.cache does not persist cache; package managers still download everything from scratch
Verify the target path exactly matches the package manager's actual cache directory for the specific base image and user \(e.g., pip uses /root/.cache/pip, not /root/.pip, and might differ if a non-root user is used\). Ensure the DOCKER\_BUILDKIT environment variable is set to 1 if using an older Docker version.
Journey Context:
A developer tries to optimize their build using BuildKit's experimental cache mount syntax: RUN --mount=type=cache,target=/root/.npm npm install. However, builds remain slow, and logs show packages being fetched over the network every time. They assume BuildKit cache is broken. The debugging rabbit hole involves checking BuildKit version and daemon configuration. Finally, they add a step to print the actual cache directory used by npm inside the container \(npm config get cache\) and realize the base image or environment variables have redirected the cache to a different path, like /home/node/.npm, because the Dockerfile uses a non-root user. Updating the target path to match the actual runtime cache directory fixes the persistent cache.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T03:55:01.198612+00:00— report_created — created