0
0
Dockerdevops~10 mins

Targeting specific stages in Docker - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Targeting specific stages
Start Docker Build
Parse Dockerfile
Identify stages
Check target stage specified?
NoBuild all stages
Yes
Build only target stage
Output image for target stage
End Build
Docker reads the Dockerfile, finds all stages, then builds only the specified target stage if given, otherwise builds all stages.
Execution Sample
Docker
FROM alpine AS base
RUN echo base

FROM base AS final
RUN echo final
This Dockerfile has two stages: 'base' and 'final'. We can build either stage separately.
Process Table
StepActionStage TargetedCommand RunOutput
1Start build with target 'base'baseRUN echo basebase
2Build stage 'base' completedbase--
3Build stops after 'base' stagebase--
4Start build with target 'final'finalRUN echo basebase
5Continue build 'final' stagefinalRUN echo finalfinal
6Build stage 'final' completedfinal--
7Build stops after 'final' stagefinal--
💡 Build stops after completing the specified target stage.
Status Tracker
VariableStartAfter Step 1After Step 4Final
Current Stagenonebasefinalfinal
Image Builtnonebase imagefinal imagefinal image
Key Moments - 2 Insights
Why does the build stop after the target stage?
Because specifying a target stage tells Docker to build only up to that stage, as shown in execution_table rows 3 and 7.
What happens if no target stage is specified?
Docker builds all stages in order, but this example focuses on targeted builds, so no target means full build.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output after step 1 when targeting 'base'?
Abase
Bfinal
Cnone
Derror
💡 Hint
Check the 'Output' column in row 1 of execution_table.
At which step does the build stop when targeting 'final'?
AStep 3
BStep 7
CStep 5
DStep 2
💡 Hint
Look for the row mentioning build stop after 'final' stage in execution_table.
If we remove the target option, how would the build behave?
ABuild only 'base' stage
BBuild only 'final' stage
CBuild all stages sequentially
DFail to build
💡 Hint
Refer to key_moments about what happens if no target stage is specified.
Concept Snapshot
Docker multi-stage builds allow naming stages.
Use --target <stage> to build only that stage.
Build stops after the target stage completes.
Without --target, all stages build in order.
This saves time and image size when only part is needed.
Full Transcript
This visual execution shows how Docker builds specific stages when using the --target option. The Dockerfile has two stages: base and final. When building with --target base, Docker runs commands only in the base stage and stops. When building with --target final, Docker builds base first (because final depends on it), then final, then stops. If no target is specified, Docker builds all stages. Variables track which stage is current and which image is built. Key moments clarify why the build stops after the target stage and what happens if no target is given. The quizzes test understanding of outputs at steps and build stopping points.