Challenge - 5 Problems
Multi-stage Mastery
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 environment-specific stages
Given this Dockerfile with two stages named
dev and prod, what will be the output of docker build --target prod . when running the container?Docker
FROM alpine AS dev RUN echo "Development build" > /build.txt FROM alpine AS prod RUN echo "Production build" > /build.txt CMD cat /build.txt FROM prod
Attempts:
2 left
💡 Hint
The final stage is
prod, so the file content comes from that stage.✗ Incorrect
The build uses the
prod stage as the final stage. The file /build.txt is created in the prod stage with content "Production build". The container runs cat /build.txt, so it outputs "Production build".❓ Configuration
intermediate2:00remaining
Correct multi-stage Dockerfile for dev and prod with shared base
Which Dockerfile correctly uses a shared base stage and separate dev and prod stages that add environment-specific files?
Attempts:
2 left
💡 Hint
Look for a shared base stage and separate dev and prod stages that copy only their config files.
✗ Incorrect
Option C correctly defines a base stage with common setup, then separate dev and prod stages that copy their specific config files and set the command. Other options either lack base stage reuse or have invalid stage references.
❓ Troubleshoot
advanced2:00remaining
Why does the prod stage fail to find a file from the base stage?
Given this Dockerfile snippet, why does the prod stage fail with "file not found" error?
FROM alpine AS base
RUN echo "Hello" > /greeting.txt
FROM alpine AS prod
COPY /greeting.txt /greeting.txt
CMD cat /greeting.txt
Attempts:
2 left
💡 Hint
COPY without --from copies from build context, not from other stages.
✗ Incorrect
COPY without --from copies files from the build context (your local folder), not from other build stages. To copy from base stage, you must use COPY --from=base /greeting.txt /greeting.txt.
🔀 Workflow
advanced2:00remaining
Best workflow to build and push dev and prod images using multi-stage Dockerfile
You have a multi-stage Dockerfile with stages
dev and prod. Which sequence of commands correctly builds and pushes both images tagged for dev and prod?Attempts:
2 left
💡 Hint
Build and push each target image separately with correct tags.
✗ Incorrect
Option D builds each target stage separately with tags and pushes both images. Option D builds without specifying target so both builds produce the same final stage image. Option D forgets to push prod image. Option D builds prod first then dev, which is valid but order is less common; however, both A and D build and push both images correctly, but only A matches the recommended order for dev then prod.
🧠 Conceptual
expert2:00remaining
Why use multi-stage builds for different environments in Docker?
What is the main advantage of using multi-stage Docker builds with separate stages for dev and prod environments?
Attempts:
2 left
💡 Hint
Think about image size and reuse of common parts.
✗ Incorrect
Multi-stage builds let you share common setup in a base stage and create separate smaller images for dev and prod by including only what each needs. This reduces image size and improves build efficiency.