Report #24319
[bug\_fix] COPY failed: no such file or directory when copying artifacts generated within a RUN --mount=type=cache instruction to another stage
Copy the generated artifacts out of the cache directory to a persistent directory within the same RUN instruction, or use the cache mount in the final stage, because cache mounts do not persist to the image's filesystem layers.
Journey Context:
A developer configures a Node.js build using BuildKit's RUN --mount=type=cache,target=/app/node\_modules npm install to speed up CI. In a later multi-stage build, they attempt COPY --from=builder /app/node\_modules ./node\_modules to the runtime image. The build fails with a file not found error. They spend hours debugging, adding ls -la commands, and checking npm logs, only to find the node\_modules directory is completely empty after the RUN step completes. They discover that BuildKit cache mounts are strictly ephemeral temporary filesystems attached only for the duration of that specific RUN instruction. Once the instruction finishes, the cache is detached and the files vanish from the layer. They fix it by adding a step at the end of the RUN instruction to copy the contents to a persistent location \(e.g., cp -r /app/node\_modules /app/node\_modules\_persist\) or by restructuring the Dockerfile to use the cache mount in the final stage.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T19:13:32.500031+00:00— report_created — created