Challenge - 5 Problems
Multi-Stage Docker Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate2:00remaining
Output of a multi-stage Docker build
Given the following Dockerfile, what will be the output when running
docker build .?Docker
FROM alpine AS builder RUN echo "Building stage" FROM alpine COPY --from=builder / /app RUN echo "Final stage"
Attempts:
2 left
💡 Hint
Remember that each FROM starts a new build stage and RUN commands execute in that stage.
✗ Incorrect
The Dockerfile uses two FROM statements. The first stage named 'builder' runs 'echo "Building stage"'. The second stage starts fresh from alpine and copies files from the builder stage, then runs 'echo "Final stage"'. The output shows the build steps in order.
🧠 Conceptual
intermediate1:30remaining
Purpose of multiple FROM statements in Dockerfile
Why do developers use multiple FROM statements in a Dockerfile?
Attempts:
2 left
💡 Hint
Think about how multi-stage builds help reduce image size.
✗ Incorrect
Multiple FROM statements allow building intermediate stages that produce build artifacts. The final stage copies only needed files, resulting in smaller images.
❓ Configuration
advanced1:30remaining
Correct COPY syntax for multi-stage build
Which COPY command correctly copies the file
/app/build/output.bin from the stage named builder to the current stage's /usr/local/bin/ directory?Attempts:
2 left
💡 Hint
The syntax uses --from=stage_name to specify the source stage.
✗ Incorrect
The correct syntax to copy from another build stage is
COPY --from=stage_name source_path destination_path. Option D follows this syntax exactly.❓ Troubleshoot
advanced2:00remaining
Error caused by missing stage name in COPY
What error will occur if you run this Dockerfile snippet?
FROM alpine AS builder RUN echo "data" > /data.txt FROM alpine COPY /data.txt /app/
Attempts:
2 left
💡 Hint
Consider where the file /data.txt exists and what the COPY command tries to do.
✗ Incorrect
The second stage starts fresh from alpine and does not have /data.txt. COPY without --from tries to copy from the current build context or image, so it fails with file not found.
🔀 Workflow
expert3:00remaining
Order of stages in a multi-stage Dockerfile
Given these Dockerfile instructions, what is the correct order to build a multi-stage Dockerfile that compiles a Go app and then creates a minimal runtime image?
1. FROM golang:1.20 AS builder
2. RUN go build -o /app/myapp ./src
3. FROM alpine:latest
4. COPY --from=builder /app/myapp /usr/local/bin/myapp
5. CMD ["myapp"]
Attempts:
2 left
💡 Hint
The build stage must come before the final stage that copies the binary.
✗ Incorrect
The builder stage starts with golang image and runs the build command. Then the final stage starts from alpine, copies the built binary from builder, and sets the CMD. The order is FROM builder, RUN build, FROM alpine, COPY, CMD.