Report #45378
[bug\_fix] COPY --from fails with no such stage or copies from the wrong context in multi-stage builds
Use named stages \(e.g., \`FROM golang:1.20 AS builder\`\) and reference them explicitly in \`COPY --from=builder\`. Avoid using integer indices unless absolutely necessary, as adding/removing stages shifts the indices.
Journey Context:
A developer has a multi-stage Dockerfile. They use \`COPY --from=0\` to copy the binary from the first stage. Later, they add a new stage at the beginning of the Dockerfile for preparing configuration files. Suddenly, the build fails or copies the wrong files. They spend hours trying to figure out why the binary is missing or corrupted. The issue is that \`--from=0\` now refers to the newly added stage, not the build stage. By using named stages like \`FROM golang:1.20 AS builder\` and \`COPY --from=builder\`, the reference becomes resilient to Dockerfile modifications. The fix works because BuildKit resolves named stages by their alias rather than their positional index.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T06:38:30.833749+00:00— report_created — created