Bird
Raised Fist0
Dockerdevops~20 mins

Multiple FROM statements in Docker - Practice Problems & Coding Challenges

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

Practice

(1/5)
1. What is the main purpose of using multiple FROM statements in a Dockerfile?
easy
A. To create multi-stage builds that reduce final image size
B. To run multiple containers at the same time
C. To specify different base images for different containers
D. To define environment variables for the container

Solution

  1. Step 1: Understand the role of FROM in Dockerfile

    The FROM statement sets the base image for building a Docker image.
  2. Step 2: Recognize multi-stage build benefits

    Using multiple FROM statements allows building in stages, copying only needed files to the final image, reducing size.
  3. Final Answer:

    To create multi-stage builds that reduce final image size -> Option A
  4. Quick Check:

    Multiple FROM = multi-stage build [OK]
Hint: Multiple FROM means multi-stage build for smaller images [OK]
Common Mistakes:
  • Thinking multiple FROM runs multiple containers
  • Confusing FROM with environment variable setting
  • Assuming FROM defines runtime commands
2. Which of the following is the correct syntax to copy files from a previous build stage named builder?
easy
A. COPY --stage=builder /app/build /app
B. COPY --from=builder /app/build /app
C. COPY --source=builder /app/build /app
D. COPY --stage-name=builder /app/build /app

Solution

  1. Step 1: Recall the correct COPY syntax for multi-stage

    The correct flag to copy from another stage is --from=stage_name.
  2. Step 2: Match the option with correct syntax

    Only COPY --from=builder /app/build /app uses --from=builder, which is valid syntax.
  3. Final Answer:

    COPY --from=builder /app/build /app -> Option B
  4. Quick Check:

    Copy from stage uses --from [OK]
Hint: Use COPY --from=stage_name to copy files between stages [OK]
Common Mistakes:
  • Using --stage or --source instead of --from
  • Misspelling the flag name
  • Omitting the equals sign (=) after --from
3. Given this Dockerfile snippet, what will be the final image size behavior?
FROM node:18 AS builder
RUN npm install && npm run build
FROM nginx:alpine
COPY --from=builder /app/build /usr/share/nginx/html
medium
A. Final image includes full Node.js and npm layers, making it large
B. Final image will fail to build due to missing /app/build directory
C. Final image only contains nginx and the built files, keeping it small
D. Final image contains both Node.js and nginx running simultaneously

Solution

  1. Step 1: Analyze the multi-stage build steps

    The first stage builds the app with Node.js. The second stage uses nginx base image.
  2. Step 2: Understand the COPY --from usage

    The built files from the builder stage are copied into nginx image, but Node.js layers are not included.
  3. Final Answer:

    Final image only contains nginx and the built files, keeping it small -> Option C
  4. Quick Check:

    Multi-stage copy keeps final image small [OK]
Hint: Final image excludes build tools if copied from previous stage [OK]
Common Mistakes:
  • Assuming all build layers stay in final image
  • Thinking COPY --from copies the entire image
  • Confusing build failure with missing files
4. Identify the error in this Dockerfile snippet using multiple FROM statements:
FROM python:3.12 AS builder
RUN pip install -r requirements.txt
FROM alpine
COPY --from=build /app /app
medium
A. The stage name 'build' does not match the defined stage 'builder'
B. Alpine image cannot be used after python image
C. COPY command syntax is incorrect; missing destination path
D. pip install command should be in the second stage

Solution

  1. Step 1: Check stage names in FROM statements

    The first stage is named 'builder', but COPY uses 'build' which is undefined.
  2. Step 2: Understand COPY --from requires exact stage name

    Stage names must match exactly; otherwise, Docker throws an error.
  3. Final Answer:

    The stage name 'build' does not match the defined stage 'builder' -> Option A
  4. Quick Check:

    Stage names must match exactly in COPY --from [OK]
Hint: Match stage names exactly in COPY --from [OK]
Common Mistakes:
  • Assuming similar names are accepted
  • Thinking base image order matters here
  • Ignoring COPY destination path correctness
5. You want to build a Docker image that compiles a Go application and then creates a minimal final image with only the compiled binary. Which Dockerfile snippet correctly uses multiple FROM statements to achieve this?
hard
A. FROM golang:1.20 AS builder WORKDIR /app COPY . . RUN go build -o myapp FROM scratch COPY --from=builder /app/myapp /myapp CMD ["/myapp"]
B. FROM golang:1.20 WORKDIR /app COPY . . RUN go build -o myapp FROM alpine COPY /app/myapp /myapp CMD ["/myapp"]
C. FROM alpine AS builder RUN apk add --no-cache go WORKDIR /app COPY . . RUN go build -o myapp FROM alpine COPY --from=builder /app/myapp /myapp CMD ["/myapp"]
D. FROM golang:1.20 AS builder WORKDIR /app COPY . . RUN go build -o myapp FROM alpine COPY --from=builder /app/myapp /myapp CMD ["/myapp"]

Solution

  1. Step 1: Identify the build stage with Go compiler

    Stage named 'builder' uses golang image to compile the app.
  2. Step 2: Use a minimal runtime image for final stage

    The final stage uses alpine (small image) and copies only the binary from builder.
  3. Step 3: Verify COPY --from syntax and CMD

    COPY uses --from=builder to get binary; CMD runs the binary correctly.
  4. Final Answer:

    FROM golang:1.20 AS builder WORKDIR /app COPY . . RUN go build -o myapp FROM alpine COPY --from=builder /app/myapp /myapp CMD ["/myapp"] -> Option D
  5. Quick Check:

    Multi-stage build with builder and minimal final image [OK]
Hint: Use builder stage for compile, minimal image for final [OK]
Common Mistakes:
  • Using scratch without proper setup
  • Copying files without --from flag
  • Building in final stage instead of builder
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard