Bird
Raised Fist0
Dockerdevops~20 mins

Targeting specific stages in Docker - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Multi-Stage Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Output of multi-stage build with specific target

Given the following Dockerfile, what will be the output of docker build --target builder .?

Docker
FROM alpine AS builder
RUN echo "Building stage" > /build.txt
FROM alpine
COPY --from=builder /build.txt /app/build.txt
CMD cat /app/build.txt
A
Step 1/2 : FROM alpine
 ---> Using cache
Step 2/2 : CMD cat /app/build.txt
 ---> Running in container
Removing intermediate container
Successfully built <image_id>
B
Step 1/2 : FROM alpine AS builder
 ---> Using cache
Step 2/2 : RUN echo "Building stage" > /build.txt
 ---> Running in container
Removing intermediate container
 ---> <image_id>
Successfully built <image_id>
C
Step 1/3 : FROM alpine
 ---> Using cache
Step 2/3 : COPY --from=builder /build.txt /app/build.txt
 ---> Running in container
Removing intermediate container
Successfully built <image_id>
D
Step 1/4 : FROM alpine AS builder
 ---> Using cache
Step 2/4 : RUN echo "Building stage" > /build.txt
 ---> Running in container
Removing intermediate container
 ---> Using cache
Successfully built <image_id>
Attempts:
2 left
💡 Hint

When you specify --target builder, Docker stops after building that stage.

🧠 Conceptual
intermediate
1:30remaining
Purpose of targeting specific stages in Docker builds

Why would a developer use the --target option to build a specific stage in a multi-stage Dockerfile?

ATo build and test intermediate stages separately without building the entire image
BTo build only the final image and skip intermediate stages
CTo automatically push the image to a registry after building
DTo reduce the size of the final image by removing unused layers
Attempts:
2 left
💡 Hint

Think about testing or debugging parts of the build process.

❓ Configuration
advanced
2:30remaining
Correct Dockerfile stage targeting syntax

Which Dockerfile snippet correctly defines two stages named builder and final so that you can target the builder stage during build?

A
FROM node:18
RUN npm install
FROM node:18
COPY --from=builder /app /app
CMD node /app/index.js
B
FROM node:18
RUN npm install
FROM node:18 AS final
COPY --from=builder /app /app
CMD node /app/index.js
C
FROM node:18 AS builder
RUN npm install
FROM node:18 AS final
COPY --from=builder /app /app
CMD node /app/index.js
D
FROM node:18 AS builder
RUN npm install
FROM node:18
COPY --from=builder /app /app
CMD node /app/index.js
Attempts:
2 left
💡 Hint

Both stages must be named to use --target properly.

❓ Troubleshoot
advanced
1:30remaining
Error when targeting a non-existent stage

What error will Docker show if you run docker build --target test . but the Dockerfile has no stage named test?

AError: failed to build: stage 'test' not found
BError: failed to solve with frontend dockerfile.v0: no match for platform in manifest
CError: unknown stage name test
DError: invalid target stage specified
Attempts:
2 left
💡 Hint

Docker complains when the target stage name is missing in the Dockerfile.

🔀 Workflow
expert
2:00remaining
Optimizing build time using targeted stages

You have a multi-stage Dockerfile with stages builder, tester, and final. You want to run tests only on the tester stage without building the final stage. Which command achieves this?

Adocker build --target final .
Bdocker build --stage tester .
Cdocker build --only tester .
Ddocker build --target tester .
Attempts:
2 left
💡 Hint

Check the correct Docker CLI option for targeting a build stage.

Practice

(1/5)
1. What does the --target <stage-name> option do when building a Docker image?
easy
A. It deletes the specified stage from the Dockerfile.
B. It builds only the specified stage in a multi-stage Dockerfile.
C. It runs the container from the specified stage.
D. It tags the image with the stage name.

Solution

  1. Step 1: Understand multi-stage Dockerfiles

    Multi-stage Dockerfiles have multiple build stages named with AS <stage-name>.
  2. Step 2: Purpose of --target

    The --target <stage-name> option tells Docker to build only up to that named stage, skipping later stages.
  3. Final Answer:

    It builds only the specified stage in a multi-stage Dockerfile. -> Option B
  4. Quick Check:

    --target builds specific stage [OK]
Hint: Use --target to build just one stage fast [OK]
Common Mistakes:
  • Thinking --target runs containers
  • Confusing --target with image tagging
  • Assuming --target deletes stages
2. Which of the following is the correct syntax to name a build stage in a Dockerfile?
easy
A. FROM ubuntu WITH builder
B. FROM ubuntu TO builder
C. FROM ubuntu STAGE builder
D. FROM ubuntu AS builder

Solution

  1. Step 1: Identify stage naming syntax

    Dockerfile uses FROM <image> AS <stage-name> to name stages.
  2. Step 2: Check options

    Only FROM ubuntu AS builder uses AS correctly to name the stage.
  3. Final Answer:

    FROM ubuntu AS builder -> Option D
  4. Quick Check:

    Stage naming uses AS keyword [OK]
Hint: Remember: FROM ... AS stageName [OK]
Common Mistakes:
  • Using TO or WITH instead of AS
  • Missing the AS keyword
  • Confusing stage naming with container commands
3. Given this Dockerfile snippet:
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 .?
medium
A. Fails because --target cannot be used with COPY --from.
B. Builds both stages and creates a final image with nginx.
C. Builds only the node:18 stage and stops before nginx stage.
D. Builds only the nginx stage and ignores node:18 stage.

Solution

  1. Step 1: Identify stages and target

    The Dockerfile has two stages: 'build' (node:18) and default (nginx:alpine). The command targets 'build'.
  2. Step 2: Effect of --target build

    Using --target build builds only the first stage and stops before the second stage.
  3. Final Answer:

    Builds only the node:18 stage and stops before nginx stage. -> Option C
  4. Quick Check:

    --target stops build at named stage [OK]
Hint: Target stage builds only that stage, skips later ones [OK]
Common Mistakes:
  • Assuming all stages build with --target
  • Thinking COPY --from breaks build
  • Confusing target stage with final image
4. You have this Dockerfile snippet:
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?
medium
A. The stage name 'test' is not defined correctly in the Dockerfile.
B. You must build the 'base' stage first before 'test'.
C. The Docker version does not support multi-stage builds.
D. The --target option cannot be used with Python images.

Solution

  1. Step 1: Check stage naming

    The Dockerfile snippet defines 'base' with AS base correctly, but the second stage FROM base lacks AS test, so 'test' is not defined.
  2. Step 2: Identify error cause

    The error "unknown stage name test" means Docker did not recognize 'test' stage due to improper definition.
  3. Final Answer:

    The stage name 'test' is not defined correctly in the Dockerfile. -> Option A
  4. Quick Check:

    Unknown stage means wrong or missing stage name [OK]
Hint: Check stage names exactly match in Dockerfile and command [OK]
Common Mistakes:
  • Assuming stages build in order automatically
  • Ignoring Docker version compatibility
  • Thinking --target depends on image type
5. You want to build only the 'final' stage from this Dockerfile:
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?
medium
A. docker build --target final .
B. docker build --target builder .
C. docker build --stage final .
D. docker build --name final .

Solution

  1. Step 1: Identify the stage to build

    The Dockerfile has two stages: 'builder' and 'final'. We want to build 'final'.
  2. Step 2: Use correct build command

    The correct syntax to build a specific stage is docker build --target <stage-name> .. So use --target final.
  3. Final Answer:

    docker build --target final . -> Option A
  4. Quick Check:

    --target final builds final stage [OK]
Hint: Use --target with exact stage name to build it [OK]
Common Mistakes:
  • Using --stage or --name instead of --target
  • Building wrong stage name
  • Forgetting the dot (.) at the end
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard