Report #600
[bug\_fix] ERROR: failed to solve: target stage production could not be found, or \`--target\` builds more stages than expected
Ensure the value passed to \`--target\` exactly matches a stage name declared with \`FROM ... AS \`. With BuildKit, only stages that the target stage depends on are built; stages that are not in the dependency graph are skipped. With the legacy builder all stages up to the target are built, so do not rely on side effects from unrelated stages.
Journey Context:
We used a multi-stage Dockerfile with \`FROM node:20 AS build\`, \`FROM nginx:alpine AS production\`, and a debug stage in between. In CI I ran \`docker build --target prod .\` and got \`target stage prod could not be found\`. I had typoed the target name; the actual stage was \`production\`. After fixing the name, I noticed BuildKit skipped the \`debug\` stage entirely. At first I worried that skipping it would break the build because the debug stage set a shared build argument, but BuildKit builds only the dependency graph reachable from the target stage. This is different from the old legacy builder, which would execute every stage up to the target. I made sure every artifact the \`production\` stage needed was produced by a stage in its dependency chain \(either directly via \`COPY --from\` or indirectly via \`FROM\`\). The fix works because BuildKit constructs an LLB directed acyclic graph and prunes unreachable stages, so the target name must be exact and dependencies must be explicit.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-13T09:58:23.600525+00:00— report_created — created