0
0
Dockerdevops~20 mins

Multiple FROM statements in Docker - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Multi-Stage Docker Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2: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"
A
Step 1/5 : FROM alpine AS builder
 ---> Using cache
Step 2/5 : RUN echo "Building stage"
 ---> Running in container
Building stage
Step 3/5 : FROM alpine
 ---> Using cache
Step 4/5 : COPY --from=builder / /app
 ---> Using cache
Step 5/5 : RUN echo "Final stage"
 ---> Running in container
Final stage
B
Step 1/3 : FROM alpine
 ---> Using cache
Step 2/3 : RUN echo "Building stage"
 ---> Running in container
Building stage
Step 3/3 : RUN echo "Final stage"
 ---> Running in container
Final stage
C
Step 1/4 : FROM alpine AS builder
 ---> Using cache
Step 2/4 : RUN echo "Final stage"
 ---> Running in container
Final stage
Step 3/4 : FROM alpine
 ---> Using cache
Step 4/4 : COPY --from=builder / /app
 ---> Using cache
Building stage
D
Step 1/4 : FROM alpine
 ---> Using cache
Step 2/4 : RUN echo "Building stage"
 ---> Running in container
Building stage
Step 3/4 : FROM alpine AS builder
 ---> Using cache
Step 4/4 : COPY --from=builder / /app
 ---> Using cache
Final stage
Attempts:
2 left
💡 Hint
Remember that each FROM starts a new build stage and RUN commands execute in that stage.
🧠 Conceptual
intermediate
1:30remaining
Purpose of multiple FROM statements in Dockerfile
Why do developers use multiple FROM statements in a Dockerfile?
ATo run multiple containers simultaneously from one Dockerfile
BTo create multiple independent images from one Dockerfile
CTo build smaller final images by copying artifacts from intermediate stages
DTo speed up the build by running all commands in parallel
Attempts:
2 left
💡 Hint
Think about how multi-stage builds help reduce image size.
Configuration
advanced
1: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?
ACOPY /app/build/output.bin --from=builder /usr/local/bin/
BCOPY builder:/app/build/output.bin /usr/local/bin/
CCOPY --stage=builder /app/build/output.bin /usr/local/bin/
DCOPY --from=builder /app/build/output.bin /usr/local/bin/
Attempts:
2 left
💡 Hint
The syntax uses --from=stage_name to specify the source stage.
Troubleshoot
advanced
2: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/
ACOPY failed: stat /data.txt: no such file or directory
BBuild succeeds and copies /data.txt from builder stage
CSyntaxError: COPY command missing --from option
DPermission denied error during COPY
Attempts:
2 left
💡 Hint
Consider where the file /data.txt exists and what the COPY command tries to do.
🔀 Workflow
expert
3: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"]
A1,3,2,4,5
B1,2,3,4,5
C3,1,2,4,5
D1,2,4,3,5
Attempts:
2 left
💡 Hint
The build stage must come before the final stage that copies the binary.