What if you could skip the slow parts and build only what matters right now?
Why Targeting specific stages in Docker? - Purpose & Use Cases
Imagine building a complex software project where you have to create different versions for testing, production, and debugging. You try to build everything at once, but it takes too long and wastes resources.
Manually building all parts every time is slow and uses too much space. You might accidentally deploy a test version to production or waste time rebuilding unchanged parts. It's easy to make mistakes and hard to fix them quickly.
Targeting specific stages lets you build only the part you need, like just the test or production version. This saves time, space, and reduces errors by focusing on one stage at a time.
docker build .
docker build --target test .
You can quickly build and deploy only the exact stage you want, making development faster and safer.
A developer builds only the 'production' stage of a Dockerfile to create a small, optimized image for deployment, skipping the heavy 'build' stage used for compiling code.
Building all stages every time wastes time and resources.
Targeting specific stages lets you build only what you need.
This approach speeds up development and reduces errors.
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
