Report #79257
[bug\_fix] COPY --from=builder fails with 'file not found' when trying to copy a directory that was populated by a previous RUN --mount=type=cache step
Do not attempt to COPY --from= directories that were targets of --mount=type=cache. Cache mounts are ephemeral and do not persist in the stage's filesystem snapshot. Instead, copy the actual built artifacts, and use the cache mount purely for the package manager's download cache.
Journey Context:
A developer tries to optimize a Go build using BuildKit cache mounts. They write RUN --mount=type=cache,target=/go/pkg/mod go build -o /app/myapp . in the builder stage. In the final stage, they attempt COPY --from=builder /go/pkg/mod /go/pkg/mod to cache the modules, but the build fails, claiming the directory doesn't exist or is empty. They are baffled because the build just succeeded. The root cause is that --mount=type=cache is a temporary overlay filesystem that is discarded from the final snapshot of the build stage. It exists only for the duration of the RUN instruction. The fix is to only copy the actual compiled binary \(COPY --from=builder /app/myapp .\) and let the cache mount handle the /go/pkg/mod directory implicitly on subsequent builds.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T15:37:40.632587+00:00— report_created — created