Challenge - 5 Problems
Named Build Stages Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate2:00remaining
Output of multi-stage Docker build with named stages
Given the Dockerfile below, what will be the output of the final image when running
docker run --rm myapp?Docker
FROM alpine AS builder RUN echo "Hello from builder" > /message.txt FROM alpine COPY --from=builder /message.txt /message.txt CMD ["cat", "/message.txt"]
Attempts:
2 left
💡 Hint
Look at how the
--from=builder copies the file from the named stage.✗ Incorrect
The first stage named 'builder' creates a file with the text 'Hello from builder'. The final stage copies that file from the builder stage and outputs its content.
❓ Configuration
intermediate1:30remaining
Correct syntax for naming a build stage
Which of the following Dockerfile snippets correctly names a build stage as 'test'?
Attempts:
2 left
💡 Hint
The keyword to name a stage is 'AS'.
✗ Incorrect
The correct syntax to name a build stage is 'FROM AS '.
❓ Troubleshoot
advanced1:30remaining
Troubleshooting COPY from a non-existent stage
What error will Docker produce if you try to copy from a named stage that does not exist?
Docker
FROM alpine COPY --from=builder /file.txt /file.txt
Attempts:
2 left
💡 Hint
Check if the stage name used in COPY matches any defined stage.
✗ Incorrect
Docker requires the stage name in COPY --from to exist. If it doesn't, it throws an error about unknown stage name.
🔀 Workflow
advanced1:30remaining
Order of stages in a multi-stage Dockerfile
In a Dockerfile with multiple named stages, which stage is used as the final image by default?
Attempts:
2 left
💡 Hint
If no target is specified, Docker uses a default stage.
✗ Incorrect
By default, Docker uses the last stage in the Dockerfile as the final image unless you specify a different target.
✅ Best Practice
expert2:30remaining
Best practice for reducing image size using named build stages
Which approach best reduces the final Docker image size when using named build stages?
Attempts:
2 left
💡 Hint
Think about separating build environment from runtime environment.
✗ Incorrect
Using a heavy builder stage with all build tools and copying only the needed files to a minimal final stage keeps the final image small and clean.