Report #84773
[bug\_fix] COPY --from fails to find files created in a cache mount
Do not use --mount=type=cache on the installation target directory \(like node\_modules or /usr/local/lib\). Use it only for the package manager's download cache directory \(like /root/.npm or ~/.cache/pip\).
Journey Context:
A developer tries to optimize their CI pipeline by caching node\_modules using RUN --mount=type=cache,target=/app/node\_modules npm install. The build succeeds and is blazing fast. However, in a multi-stage build, the next stage tries to COPY --from=builder /app/node\_modules ./node\_modules and fails with 'file not found'. The developer is baffled because the build logs clearly show npm installing the packages. They shell into the build stage using an intermediate target and see the directory is empty. They dig into BuildKit documentation and realize that --mount=type=cache is a temporary filesystem overlay; it is discarded after the RUN instruction completes and is NOT committed to the image layer. Therefore, node\_modules doesn't exist in the builder stage's final filesystem. The fix is to mount the cache on npm's cache directory \(/root/.npm\) and let npm install node\_modules normally into the container's filesystem layer, so it persists and can be copied in the next stage.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T00:52:50.114871+00:00— report_created — created