Targeting specific stages in Docker - Time & Space Complexity
When building Docker images with multiple stages, it's important to know how targeting a specific stage affects build time.
We want to understand how the build time changes as the number of stages grows.
Analyze the time complexity of this multi-stage Dockerfile snippet.
FROM node:18 AS base
RUN npm install
FROM base AS build
RUN npm run build
FROM build AS test
RUN npm test
FROM build AS final
CMD ["node", "app.js"]
This Dockerfile has four stages. We can target any stage to build only up to that point.
Look for repeated steps or commands that run multiple times.
- Primary operation: Running each stage's commands sequentially during build.
- How many times: Once per stage up to the targeted stage.
As the number of stages increases, building a specific stage requires running all previous stages too.
| Number of Stages (n) | Approx. Commands Run |
|---|---|
| 2 | 2 |
| 4 | 4 |
| 10 | 10 |
Pattern observation: The build time grows roughly in direct proportion to the number of stages built.
Time Complexity: O(n)
This means the build time grows linearly with the number of stages you build up to.
[X] Wrong: "Building a later stage only runs that stage's commands."
[OK] Correct: Docker must run all earlier stages first to prepare the environment, so build time includes all previous stages.
Understanding how build time scales with stages helps you design efficient Dockerfiles and explain your choices clearly in real projects.
"What if Docker cached some stages? How would that affect the time complexity when targeting a specific stage?"
Practice
--target <stage-name> option do when building a Docker image?Solution
Step 1: Understand multi-stage Dockerfiles
Multi-stage Dockerfiles have multiple build stages named withAS <stage-name>.Step 2: Purpose of
The--target--target <stage-name>option tells Docker to build only up to that named stage, skipping later stages.Final Answer:
It builds only the specified stage in a multi-stage Dockerfile. -> Option BQuick Check:
--target builds specific stage [OK]
- Thinking --target runs containers
- Confusing --target with image tagging
- Assuming --target deletes stages
Solution
Step 1: Identify stage naming syntax
Dockerfile usesFROM <image> AS <stage-name>to name stages.Step 2: Check options
Only FROM ubuntu AS builder usesAScorrectly to name the stage.Final Answer:
FROM ubuntu AS builder -> Option DQuick Check:
Stage naming uses AS keyword [OK]
- Using TO or WITH instead of AS
- Missing the AS keyword
- Confusing stage naming with container commands
FROM node:18 AS build RUN npm install FROM nginx:alpine COPY --from=build /app/build /usr/share/nginx/html
What happens if you run
docker build --target build .?Solution
Step 1: Identify stages and target
The Dockerfile has two stages: 'build' (node:18) and default (nginx:alpine). The command targets 'build'.Step 2: Effect of --target build
Using--target buildbuilds only the first stage and stops before the second stage.Final Answer:
Builds only the node:18 stage and stops before nginx stage. -> Option CQuick Check:
--target stops build at named stage [OK]
- Assuming all stages build with --target
- Thinking COPY --from breaks build
- Confusing target stage with final image
FROM python:3.12 AS base RUN pip install flask FROM base RUN pip install pytest
You run
docker build --target test . but get an error: "unknown stage name test". What is the likely cause?Solution
Step 1: Check stage naming
The Dockerfile snippet defines 'base' with AS base correctly, but the second stageFROM baselacksAS test, so 'test' is not defined.Step 2: Identify error cause
The error "unknown stage name test" means Docker did not recognize 'test' stage due to improper definition.Final Answer:
The stage name 'test' is not defined correctly in the Dockerfile. -> Option AQuick Check:
Unknown stage means wrong or missing stage name [OK]
- Assuming stages build in order automatically
- Ignoring Docker version compatibility
- Thinking --target depends on image type
FROM golang:1.20 AS builder RUN go build -o app . FROM alpine:latest AS final COPY --from=builder /app /app CMD ["/app"]
Which command correctly builds only the 'final' stage?
Solution
Step 1: Identify the stage to build
The Dockerfile has two stages: 'builder' and 'final'. We want to build 'final'.Step 2: Use correct build command
The correct syntax to build a specific stage isdocker build --target <stage-name> .. So use--target final.Final Answer:
docker build --target final . -> Option AQuick Check:
--target final builds final stage [OK]
- Using --stage or --name instead of --target
- Building wrong stage name
- Forgetting the dot (.) at the end
Ready to prove it?
Pass the test and this pattern moves unknown
