Building Multi-Stage Dockerfiles for Small Production Images
Use multi-stage builds so compilers stay out of the final runtime image.
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/htmlfor 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
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.
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, 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.
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.
How it works, step by step
Step 1 of 41. 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.
- 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 binariesin 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
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.
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.
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/htmlas proof and skippingcompare 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/htmladdress 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?
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
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.