Sub topic 9
Naming stages and building a single target
This chapter assumes you can read a multi-stage Dockerfile with FROM ... AS and COPY --from, and that you have BuildKit available. BuildKit is the default builder in current Docker releases. Here you will name stages on purpose, refer to them by name, and stop a build at the stage you want.
Why name stages
Every FROM line starts a new stage. Without a name, Docker numbers stages from 0, and you would have to write COPY --from=1. Adding AS name fixes this. If you insert or reorder a stage later, numeric references silently point at the wrong stage. Named references do not.
Use short, lowercase names that say what the stage does, such as deps, build, test and runtime.
A Dockerfile with several named stages
The stages below share a deps stage. The test and build stages both start from it with FROM deps. A stage name works as a base image name.
FROM node:20 AS deps
WORKDIR /app
COPY package*.json ./
RUN npm ci
FROM deps AS test
COPY . .
RUN npm test
FROM deps AS build
COPY . .
RUN npm run build
FROM node:20-slim AS runtime
WORKDIR /app
ENV NODE_ENV=production
COPY package*.json ./
RUN npm ci --omit=dev
COPY --from=build /app/dist ./dist
USER node
CMD ["node", "dist/index.js"]The runtime stage never refers to test. Nothing in the final image depends on the tests, so tests can fail without affecting how runtime is built.
Building a single target
By default, docker build produces the last stage in the file. The --target flag stops at a stage you name.
# Build the final stage (runtime)
docker build -t myapp:latest .
# Run only the tests
docker build --target test -t myapp:test .
# Build up to the compile step and inspect the result
docker build --target build -t myapp:build .With BuildKit, Docker works out which stages the target depends on and skips the rest. Building runtime runs deps and build but not test. Building test runs only deps and test. Independent stages can also run in parallel.
If the test stage's npm test fails, docker build --target test exits with a non-zero status. That makes it a convenient CI step without a separate test image in production.
Common uses for --target
- Run tests or linters in CI without producing a runtime image.
- Build a
devstage that includes debuggers and other tools, and keep it out of the production stage. - Inspect a build stage by building it and starting a shell in it with
docker run --rm -it myapp:build sh.
Common errors
- Unknown stage name. A typo in
--targetorCOPY --fromfails with an error saying the stage could not be found. Check that theASname matches exactly. - Referencing a later stage.
COPY --fromcan only use stages defined earlier in the file. - Expecting a skipped stage to run. If no target depends on a stage, BuildKit does not execute it. Putting a
RUNcheck in a side stage that nothing depends on will not run it during a normal build. Build it explicitly with--target.
Exercise
Using the Dockerfile in this chapter, which stages run when you execute docker build --target test -t myapp:test . with BuildKit? Which stages are skipped?
Show answer
The deps and test stages run, because test starts FROM deps. The build and runtime stages are skipped, since the test target does not depend on them.
Exercise
Why is COPY --from=build /app/dist ./dist safer than COPY --from=1 /app/dist ./dist?
Show answer
Stage numbers depend on position in the file. Adding or reordering a stage changes the numbers and can make the instruction copy from the wrong stage. A name stays attached to its stage.