IntermediatePhone
2 min
Docker Multi-stage Builds
DockerContainersBuild Systems
Advertisement
Interview Question
What are Docker multi-stage builds and why are they useful?
Key Points to Cover
- Use multiple FROM stages to separate build and runtime
- Reduces final image size and attack surface
- Improves caching and reproducibility of builds
Evaluation Rubric
Explains multi-stage concept34% weight
Describes size/security benefits33% weight
Mentions caching/reproducibility33% weight
Hints
- 💡Think: builder image → copy artifacts to skinny runtime.
Common Pitfalls to Avoid
- ⚠️Copying unnecessary files from a build stage (e.g., entire source directories) instead of just the final compiled artifacts, negating the size reduction benefits.
- ⚠️Failing to alias build stages (e.g., `FROM ... AS builder`), which makes the Dockerfile harder to read, maintain, and reference specific stages.
- ⚠️Using a large, unoptimized base image for the final runtime stage (e.g., `ubuntu` or `node:latest`) instead of a minimal one (e.g., `alpine`, `scratch`, or a slim version), which diminishes the overall image size reduction.
- ⚠️Incorrectly assuming artifacts are available before they are built in a preceding stage, leading to build failures when `COPY --from` is executed prematurely.
- ⚠️Misunderstanding how caching works across stages, potentially leading to unnecessary rebuilds if the build process doesn't correctly leverage the independent layer caching.
Potential Follow-up Questions
- ❓How do you pass artifacts between stages?
- ❓How to cache dependencies effectively?
Advertisement