Skip to main content

What does "multi-stage build" mean?

"Multi-stage build" is a way to build a Docker image using several stages (steps) in a single Dockerfile. One stage builds the project (for example, installs dependencies, compiles code), while another uses only the build's result, without carrying over all the clutter from the first stage.

What multi-stage build gives you:

1. A smaller final image The final stage copies only the needed artifact (for example, a binary or the built frontend bundle), without the SDK, compilers, or temporary files.

2. A more secure image At runtime, the application won't have development tools, build utilities, or extra dependencies - a smaller attack surface.

3. Easier to maintain Everything is in one Dockerfile, but split into stages: build, test, prod, etc.

A short example:

dockerfile
## Stage 1 - build FROM node:18 AS builder WORKDIR /app COPY . . RUN npm install && npm run build # Stage 2 - lightweight final image FROM nginx:alpine COPY --from=builder /app/dist /usr/share/nginx/html

The result is a small, clean final image that contains only what's needed to run.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.