0
0
Dockerdevops~20 mins

Targeting specific stages in Docker - Practice Problems & Coding Challenges

Choose your learning style9 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.