Report #91070
[bug\_fix] Cache key not found or cache not being shared/reused across steps when using RUN --mount=type=cache
Ensure the target path in --mount=type=cache,target=... exactly matches the directory the package manager uses, and explicitly define an id parameter \(e.g., id=npm-cache\) to share the cache across different RUN steps or Dockerfile changes.
Journey Context:
A developer migrates to BuildKit to speed up CI using RUN --mount=type=cache,target=/root/.npm npm install. The build runs but doesn't hit the cache, or throws a cache key error on subsequent runs. The rabbit hole involves checking CI cache volumes and Docker daemon storage drivers. They realize that without an explicit id, BuildKit generates a cache ID based on the target path and the exact content hash of the Dockerfile line. If the Dockerfile changes slightly \(e.g., adding an environment variable before the RUN command\), the content hash changes, breaking the cache. By setting an explicit id \(e.g., --mount=type=cache,id=npm-cache,target=/root/.npm\), the cache persists across Dockerfile changes. The fix works because explicit IDs decouple the cache lifecycle from the specific instruction's content hash.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T11:27:27.259828+00:00— report_created — created