ERROR: failed to build: failed to solve: target stage "finall" could not be found (did you mean final?)
means the name after --target matches no FROM ... AS name line in the
Dockerfile. Use one of the stage names the file declares. In the test
finall got the hint (did you mean final?), and prod
got no hint.
In the test --target final built the image, and running it printed what the
final stage copies. The stage names live on the FROM lines, so read those
lines when the hint is missing.
The error
docker build --progress=plain --target finall -t fix12-dkbuild-b . 2>&1
#0 building with "desktop-linux" instance using docker driver
#1 [internal] load build definition from Dockerfile
#1 transferring dockerfile: 182B 0.0s done
#1 DONE 0.1s
ERROR: failed to build: failed to solve: target stage "finall" could not be found (did you mean final?)
exit=1
Why it happens
The test file has two stages, build and final, named by
AS on their FROM lines. finall is neither, so the
build stopped right after reading the Dockerfile: step #1 is the last one in
the output, and no base image metadata was loaded. With --target build the
build ran the two steps of the first stage and had no [final 2/2] step.
The hint depended on the name. With --target prod the line ended at
target stage "prod" could not be found with no suggestion. With
--target FINAL in capitals the build did not fail at all: it built the
final stage and ended with exit code 0. So a wrong case was not the cause in
this test; a wrong spelling was.
The fix
FROM alpine:latest AS build
RUN echo built > /out.txt
FROM alpine:latest AS final
COPY --from=build /out.txt /out.txt
CMD ["cat", "/out.txt"]
docker build --progress=plain --target final -t fix12-dkbuild-b . 2>&1
...
#7 naming to docker.io/library/fix12-dkbuild-b:latest done
#7 unpacking to docker.io/library/fix12-dkbuild-b:latest 0.0s done
#7 DONE 0.2s
exit=0
The Dockerfile above is the whole test file and did not change; only the
--target value did. The image then ran, as
cmd /c "docker run --rm --name fix12-dkbuild-runb fix12-dkbuild-b 2>&1"
from PowerShell, and printed built, the text the first stage wrote and the
final stage copied.
How it was reproduced
The two-stage Dockerfile above on the cached alpine:latest (image id
294b683cb724), built on Docker Desktop 4.99.0 for Windows with client and engine 29.9.3 and
buildx v0.38.1 on the desktop-linux builder. Each command ran in
cmd, started from PowerShell as cmd /c "... 2>&1", and the
exit code was printed after it. Five builds were run: finall,
prod, FINAL, final and build. The folder
lab\multi, the stage names build and final, the
container name fix12-dkbuild-runb and the image tags
fix12-dkbuild-b, -b2 and -b3 are the test's own.
Frequently asked
- What does 'target stage ... could not be found' mean in docker build?
- The name given to --target is not the name of any stage in the Dockerfile. Stage names are set with AS on the FROM lines.
- Is docker build --target case sensitive?
- Not in this test: --target FINAL built the stage declared as AS final and ended with exit code 0. A misspelled name failed.
- How do I list the stages in a Dockerfile?
- Read the FROM lines: each FROM image AS name declares a stage called name. In the test those were build and final, and both worked with --target.
More decoded errors in the Fixes category. For a
misspelled stage name in COPY --from, see
COPY --from a misspelled stage.