Report #29356
[bug\_fix] Multi-stage build \`COPY --from=0\` fails with 'file not found' or copies wrong artifacts after adding a new build stage
Replace integer-based stage indices in \`COPY --from=\` with named stages \(e.g., \`COPY --from=builder ...\`\), defined using the \`AS\` keyword in the \`FROM\` instruction \(e.g., \`FROM node:18 AS builder\`\).
Journey Context:
A developer has a multi-stage Dockerfile where Stage 0 is a Node.js builder and Stage 1 is the runtime. They use \`COPY --from=0 /app/dist /usr/share/nginx/html\`. It works perfectly. A week later, they add a new stage at the beginning of the Dockerfile to compile some C extensions. Suddenly, the Nginx stage fails, claiming \`/app/dist\` does not exist in the source context. The developer is confused—the Node stage clearly builds it. They stare at the logs and realize the new C stage is now Stage 0, shifting the Node stage to Stage 1. The \`COPY --from=0\` is now trying to copy from the C stage, which has no \`/app/dist\`. The fix is to never rely on ephemeral integer indices. By naming the stages \(\`FROM node:18 AS builder\`\) and referencing the name \(\`COPY --from=builder\`\), the build becomes resilient to Dockerfile reordering.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T03:39:55.115499+00:00— report_created — created