Report #64687
[bug\_fix] Multi-stage build copies entire build environment instead of just the compiled artifact, resulting in a massive final image
Name the build stages \(e.g., \`FROM golang:1.20 AS builder\`\) and explicitly copy only the built artifact \(e.g., \`COPY --from=builder /app/bin /app/bin\`\) to the final stage.
Journey Context:
A developer creates a multi-stage build to reduce image size. Stage 1 compiles a Go binary. Stage 2 uses \`alpine\`. They use \`COPY --from=0 / . .\` and the resulting image is still 800MB instead of 20MB. They investigate the layers and see development dependencies and source code were copied over. They try to copy specific paths but miss the binary. The fix is to use named stages \(\`AS builder\`\) instead of indices \(\`0\`\) for clarity, and specifically copy the exact output artifact path \(\`COPY --from=builder /go/src/app/app /app\`\). This works because named stages prevent off-by-one errors if Dockerfiles are reordered, and explicit artifact paths ensure only the compiled binary enters the final scratch/alpine image, shedding the build dependencies.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T15:03:52.233602+00:00— report_created — created