0
0
Dockerdevops~20 mins

Multi-stage builds concept in Docker - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Multi-stage Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Purpose of multi-stage builds in Docker

Why do we use multi-stage builds in Dockerfiles?

ATo enable Dockerfiles to run on multiple operating systems automatically
BTo run multiple containers simultaneously from one Dockerfile
CTo reduce the final image size by copying only necessary artifacts from build stages
DTo speed up container startup by preloading all dependencies in one stage
Attempts:
2 left
💡 Hint

Think about how multi-stage builds help with image size and efficiency.

💻 Command Output
intermediate
2:00remaining
Output of multi-stage Docker build command

What will be the output of the following Docker build command if the Dockerfile has two stages named builder and final, and the final stage copies files from builder?

docker build -t myapp .
Docker
FROM node:18 AS builder
WORKDIR /app
COPY package.json .
RUN npm install
COPY . .
RUN npm run build

FROM nginx:alpine AS final
COPY --from=builder /app/build /usr/share/nginx/html
ABuild completes successfully creating an image 'myapp' with only the nginx server and built files
BBuild fails due to missing RUN command in the final stage
CBuild completes but the image contains both node and nginx layers increasing size
DBuild fails because the final stage cannot access files from the builder stage
Attempts:
2 left
💡 Hint

Consider how --from=builder works in multi-stage builds.

Configuration
advanced
2:30remaining
Correct multi-stage Dockerfile to build and run a Go app

Which Dockerfile correctly uses multi-stage builds to compile a Go app and produce a minimal final image?

A
FROM golang:1.20 AS builder
WORKDIR /app
COPY . .
RUN go build -o app

FROM alpine:latest
COPY --from=builder /app/app /app
CMD ["/app"]
B
FROM alpine:latest
WORKDIR /app
COPY . .
RUN go build -o app
CMD ["./app"]
C
FROM golang:1.20
WORKDIR /app
COPY . .
RUN go build -o app
CMD ["./app"]
D
FROM alpine:latest AS builder
WORKDIR /app
COPY . .
RUN go build -o app

FROM golang:1.20
COPY --from=builder /app/app /app
CMD ["/app"]
Attempts:
2 left
💡 Hint

Remember the builder stage needs Go tools, and the final image should be minimal.

Troubleshoot
advanced
2:00remaining
Why does the final image not contain expected files?

You wrote a multi-stage Dockerfile where the final stage copies files from the builder stage, but after building, the final image is missing those files. What is the most likely cause?

ADocker does not support copying files between stages
BThe <code>--from=builder</code> path is incorrect or the files were not created in the builder stage
CThe final stage must use the same base image as the builder stage to copy files
DThe build command was missing the <code>--target</code> option to specify the final stage
Attempts:
2 left
💡 Hint

Check the paths and build steps in the builder stage.

🔀 Workflow
expert
3:00remaining
Optimizing CI pipeline with multi-stage Docker builds

You want to optimize your CI pipeline to build Docker images faster using multi-stage builds. Which approach will best speed up builds while keeping images small?

AUse a single-stage build with all dependencies installed to avoid complexity
BBuild all stages separately as independent images and then merge them manually
CDisable Docker layer caching to ensure fresh builds every time
DUse multi-stage builds with caching enabled for dependencies in the builder stage and copy only final artifacts to the last stage
Attempts:
2 left
💡 Hint

Think about caching and minimizing layers in multi-stage builds.