Report #104289
[bug\_fix] Multi-stage build: COPY --from=stage\_name fails with 'target stage not found'
Ensure the stage name used in \`COPY --from=\` matches exactly the name given in \`FROM ... AS \`. The stage name is case-sensitive and must be defined before the COPY instruction. If using a numeric index \(e.g., \`COPY --from=0\`\), verify the stage order. Also check that the stage is not skipped due to conditional builds \(e.g., \`FROM ... AS builder\` but builder is never used\).
Journey Context:
A developer was refactoring a Dockerfile into multi-stage builds to reduce image size. They had \`FROM python:3.11-slim AS builder\` and later \`COPY --from=builder /app/dist /app/dist\`. The build failed with 'target stage builder not found'. After checking the Dockerfile multiple times, they realized that the \`FROM python:3.11-slim AS builder\` line was inside a conditional \`ARG\` block that was not evaluated at build time because the \`ARG\` was not passed. The real root cause: BuildKit does not evaluate ARGs in \`FROM\` statements unless the ARG is declared before the first FROM and provided via \`--build-arg\`. The fix: declare the ARG before the first FROM, e.g., \`ARG BUILD\_STAGE\`, and then use \`FROM python:3.11-slim AS $\{BUILD\_STAGE:-builder\}\`. They also ensured the stage name was consistent and not misspelled.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-07-26T20:04:54.913872+00:00— report_created — created