Report #45927
[bug\_fix] Multi-stage build COPY --from fails or copies from wrong stage after Dockerfile refactor
Name your build stages using the AS keyword \(e.g., FROM node:18 AS builder\) and reference the name in COPY --from=builder instead of using integer indices like COPY --from=0. Integer indices are fragile and break when stages are added, removed, or reordered.
Journey Context:
A developer has a multi-stage Dockerfile. Stage 0 is a Node.js builder, and Stage 1 is an Nginx server. They use COPY --from=0 /app/dist /usr/share/nginx/html. Later, they decide to add a linting stage at the very beginning of the Dockerfile. The build suddenly fails with 'file not found' in the Nginx stage, or worse, copies the wrong files entirely. They spend time checking the build outputs of the Node stage, confused as to why /app/dist is missing. They eventually realize that adding the linting stage shifted all the indices, so --from=0 now points to the linting stage, and the Node builder is now --from=1. To prevent this fragile, error-prone situation in the future, they refactor the Dockerfile to use named stages \(FROM node:18 AS builder\) and update the copy command to COPY --from=builder. This makes the Dockerfile self-documenting and resilient to future structural changes.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T07:33:46.833665+00:00— report_created — created