Interview's MateDocker
Interview's Mate

Docker

Compose · Networking · Security · Ops

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
Docker topics
Lesson 33/ 160
02. Dockerfiles & Image Building

Building Multi-Stage Dockerfiles for Small Production Images

Use multi-stage builds so compilers stay out of the final runtime image.

IntermediateMulti-Stage · Builder3 quiz questions
Practice DevOps Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

In plain words

Use several stages when build-time dependencies are materially larger or riskier than runtime dependencies.

Think of it like…

The workshop can contain saws and glue; the delivered cabinet contains only the finished piece and the screws needed to mount it.

Where you’ve seen it

In practice, FROM node:22-bookworm AS build; RUN npm ci && npm run build; FROM nginx:1.27-alpine; COPY --from=build /app/dist /usr/share/nginx/html is the concrete move for this job: use multi-stage builds so compilers stay out of the final runtime image. The release is not accepted until compare docker image size before and after and ensure docker run can serve the built index without build-only binaries confirms the result.

Words you’ll hear

Multi-Stage
The implementation control in FROM node:22-bookworm AS build; RUN npm ci && npm run build; FROM nginx:1.27-alpine; COPY --from=build /app/dist /usr/share/nginx/html for use multi-stage builds so compilers stay out of the final runtime image.
Builder
The result to observe with compare docker image size before and after and ensure docker run can serve the built index without build-only binaries, rather than infer from a successful command alone.
Runtime
The production boundary exposed when copying only the compiled binary for a dynamically linked service, or copying a node_modules tree that contains native modules from a different libc.

Visual diagram

Loading interactive diagram

Theory & deep dive

Multi-Stage control path

The concrete mechanism is FROM node:22-bookworm AS build; RUN npm ci && npm run build; FROM nginx:1.27-alpine; COPY --from=build /app/dist /usr/share/nginx/html. The observable result is compare docker image size before and after and ensure docker run can serve the built index without build-only binaries; if those disagree, inspect the runtime evidence before changing unrelated settings.

Use multi-stage builds so compilers stay out of the final runtime image. Keep Multi-Stage, Builder and Runtime in the same deployment record so the intent and the observed state cannot drift apart.

Concrete Multi-Stage setup

FROM node:22-bookworm AS build; RUN npm ci && npm run build; FROM nginx:1.27-alpine; COPY --from=build /app/dist /usr/share/nginx/html

Key takeaway: Prove Builder with compare docker image size before and after and ensure docker run can serve the built index without build-only binaries, not with command success alone.

Runtime failure boundary

A single-stage Node, Go or Java image often ships source, compilers, package caches and credentials the runtime never needs. Multi-stage builds still require copying CA roots, assets, native libraries and a compatible non-root user into the final stage. The narrow failure to design for is copying only the compiled binary for a dynamically linked service, or copying a node_modules tree that contains native modules from a different libc.

Accept this compromise explicitly: Multiple stages create small production images but add Dockerfile complexity and separate cache graphs to reason about. The controlled check-compare docker image size before and after and ensure docker run can serve the built index without build-only binaries-also identifies the point where a different design is safer.

Deliberate failure test

Exercise this failure safely: copying only the compiled binary for a dynamically linked service, or copying a node_modules tree that contains native modules from a different libc.

Key takeaway: Multiple stages create small production images but add Dockerfile complexity and separate cache graphs to reason about.

How it works, step by step

Step 1 of 4
Design

1. State the boundary

Write down why the workload needs this: A single-stage Node, Go or Java image often ships source, compilers, package caches and credentials the runtime never needs. Multi-stage builds still require copying CA roots, assets, native libraries and a compatible non-root user into the final stage.

Use several stages when build-time dependencies are materially larger or riskier than runtime dependencies.
Key numbers at this step
Intent
Multi-Stage

When to use it & trade-offs

What it is

A multi-stage Dockerfile names independent FROM stages, runs compilers and package managers in a builder, then copies only the required binary or build output into a clean runtime stage. Stage boundaries let cacheable build tooling disappear from the final image.

Why it matters: A single-stage Node, Go or Java image often ships source, compilers, package caches and credentials the runtime never needs. Multi-stage builds still require copying CA roots, assets, native libraries and a compatible non-root user into the final stage.

When to reach for it

  • The fit is strongest for this job: use multi-stage builds so compilers stay out of the final runtime image. Pair it with compare docker image size before and after and ensure docker run can serve the built index without build-only binaries in the runbook.
  • Production Multi-Stage changes should make Builder observable through FROM node:22-bookworm AS build; RUN npm ci && npm run build; FROM nginx:1.27-alpine; COPY --from=build /app/dist /usr/share/nginx/html, rather than trusting the runtime default.
  • Keep a rollback example for the case where copying only the compiled binary for a dynamically linked service, or copying a node_modules tree that contains native modules from a different libc.

When to avoid or be careful

  • Do not accept command success while the system can still fail by copying only the compiled binary for a dynamically linked service, or copying a node_modules tree that contains native modules from a different libc.
  • Avoid erasing Runtime protection merely to dodge this cost: “Multiple stages create small production images but add Dockerfile complexity and separate cache graphs to reason about”.
  • Do not replace the evidence path-compare docker image size before and after and ensure docker run can serve the built index without build-only binaries-with a screenshot or a mutable tag.

FROM node:22-bookworm AS build; RUN npm ci && npm run build; FROM nginx:1.27-alpine; COPY --from=build /app/dist /usr/share/nginx/html: practical choices

Explicit Multi-Stage
Best for: Use multi-stage builds so compilers stay out of the final runtime image, with a reviewed runbook.

Strengths: A multi-stage Dockerfile names independent FROM stages, runs compilers and package managers in a builder, then copies only the required binary or build output into a clean runtime stage. Stage boundaries let cacheable build tooling disappear from the final image.

Watch out: Multiple stages create small production images but add Dockerfile complexity and separate cache graphs to reason about.

Ambient default
Best for: Short-lived Builder experiments with low blast radius.

Strengths: It can begin with FROM node:22-bookworm AS build; RUN npm ci && npm run build; FROM nginx:1.27-alpine; COPY --from=build /app/dist /usr/share/nginx/html and fewer surrounding controls.

Watch out: It silently accepts copying only the compiled binary for a dynamically linked service, or copying a node_modules tree that contains native modules from a different libc, which makes the result hard to reproduce after promotion.

Managed Runtime
Best for: Teams that centralize Runtime policy across many workloads.

Strengths: A single-stage Node, Go or Java image often ships source, compilers, package caches and credentials the runtime never needs. Multi-stage builds still require copying CA roots, assets, native libraries and a compatible non-root user into the final stage.

Watch out: The platform adds an ownership and debugging layer; retain compare docker image size before and after and ensure docker run can serve the built index without build-only binaries as the direct check.

Interview signals & numbers to know

When this topic comes up

  • FROM node:22-bookworm AS build; RUN npm ci && npm run build; FROM nginx:1.27-alpine; COPY --from=build /app/dist /usr/share/nginx/html
  • compare docker image size before and after and ensure docker run can serve the built index without build-only binaries
  • Failure signature: copying only the compiled binary for a dynamically linked service, or copying a node_modules tree that contains native modules from a different libc
  • Trade-off: Multiple stages create small production images but add Dockerfile complexity and separate cache graphs to reason about.

Benchmarks & rules of thumb

2+ stages -
builder and runtime are the minimum pattern
0 compiler files -
desired in the final application layer
1 explicit COPY --from -
for each output crossing a stage boundary

Common mistakes & follow-ups

Common interview mistakes

  • Treating a successful FROM node:22-bookworm AS build; RUN npm ci && npm run build; FROM nginx:1.27-alpine; COPY --from=build /app/dist /usr/share/nginx/html as proof and skipping compare docker image size before and after and ensure docker run can serve the built index without build-only binaries.
  • Copying only the compiled binary for a dynamically linked service, or copying a node_modules tree that contains native modules from a different libc
  • Optimizing Multi-Stage without accepting that multiple stages create small production images but add Dockerfile complexity and separate cache graphs to reason about.

Likely interviewer follow-ups

  • Why does FROM node:22-bookworm AS build; RUN npm ci && npm run build; FROM nginx:1.27-alpine; COPY --from=build /app/dist /usr/share/nginx/html address Multi-Stage here rather than through application code?
  • How would you distinguish healthy Builder from the failure “copying only the compiled binary for a dynamically linked service, or copying a node_modules tree that contains native modules from a different libc” using compare docker image size before and after and ensure docker run can serve the built index without build-only binaries?
  • When would this trade-off-“Multiple stages create small production images but add Dockerfile complexity and separate cache graphs to reason about”-justify a different Runtime design?
Interview tip: Give one concrete operation-FROM node:22-bookworm AS build; RUN npm ci && npm run build; FROM nginx:1.27-alpine; COPY --from=build /app/dist /usr/share/nginx/html-and one proof-compare docker image size before and after and ensure docker run can serve the built index without build-only binaries. Then name the boundary: copying only the compiled binary for a dynamically linked service, or copying a node_modules tree that contains native modules from a different libc.

Knowledge check

Loading quiz

Put it into practice

Practice related interview Q&A

Apply Building Multi-Stage Dockerfiles for Small Production Images with generative AI interview Q&A and hands-on practice.

Open DevOps Q&A →Tech Learn Q&A
Previous topic
Tagging Images Properly (Semantic Versioning, latest Anti-Pattern)
Next topic
Choosing Minimal Base Images (alpine, slim, distroless, chainguard)