0
0
MLOpsdevops~20 mins

Multi-stage builds for smaller images in MLOps - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Multi-stage Build Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Purpose of Multi-stage Builds

Why do we use multi-stage builds in Dockerfiles when creating container images?

ATo reduce the final image size by separating build and runtime environments
BTo increase the build time by adding more steps
CTo make the image compatible only with specific operating systems
DTo add multiple entry points to the container
Attempts:
2 left
💡 Hint

Think about how build tools and runtime dependencies differ.

💻 Command Output
intermediate
2:00remaining
Output of Multi-stage Docker Build

Given this Dockerfile snippet, what will be the size difference between the final image and the build stage image?

FROM python:3.12-slim AS build
RUN pip install --no-cache-dir numpy

FROM python:3.12-slim
COPY --from=build /usr/local/lib/python3.12/site-packages /usr/local/lib/python3.12/site-packages
CMD ["python3"]
ABoth images have the same size
BFinal image is larger because it includes all build dependencies
CFinal image is smaller because it excludes build tools and caches
DFinal image will fail to build due to missing dependencies
Attempts:
2 left
💡 Hint

Consider what is copied from the build stage to the final stage.

Configuration
advanced
2:30remaining
Correct Multi-stage Dockerfile Syntax

Which Dockerfile snippet correctly uses multi-stage builds to compile a Go application and produce a small final image?

A
FROM alpine:latest AS build
WORKDIR /app
COPY . .
RUN go build -o app

FROM golang:1.20
COPY --from=build /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 golang:1.20 AS build
WORKDIR /app
COPY . .
RUN go build -o app

FROM alpine:latest
COPY --from=build /app/app /app
CMD ["/app"]
Attempts:
2 left
💡 Hint

Remember the build stage needs the Go compiler, but the final image should be minimal.

Troubleshoot
advanced
2:00remaining
Troubleshooting Missing Files in Final Image

You created a multi-stage Dockerfile but the final image is missing some files needed at runtime. What is the most likely cause?

AFiles were not copied from the build stage to the final stage using <code>COPY --from=build</code>
BThe base image in the final stage is too large
CThe build stage did not run any commands
DThe Dockerfile is missing a <code>CMD</code> instruction
Attempts:
2 left
💡 Hint

Think about how files move between stages in multi-stage builds.

🔀 Workflow
expert
3:00remaining
Optimizing CI Pipeline with Multi-stage Builds

You want to optimize your CI pipeline to build and test a Node.js app using multi-stage Docker builds. Which workflow best reduces build time and image size?

AInstall all dependencies and run tests in the final stage only
BUse a build stage to install dependencies and run tests, then copy only production files to final stage
CRun tests outside Docker, then build a single-stage Docker image with all files
DUse multiple final stages each with different dependencies for testing and production
Attempts:
2 left
💡 Hint

Consider how multi-stage builds can separate testing and production environments.