Multi-stage builds and small images
A naive image for a React or Go app can be over 1 GB, full of compilers, dev dependencies and source code that production never needs. Smaller images deploy faster, cost less to store, start quicker, and have fewer security vulnerabilities.
The problem
# ✗ One-stage build: ships the whole toolchain
FROM node:22
WORKDIR /app
COPY . .
RUN npm install && npm run build
CMD ["npx", "serve", "dist"]This image contains Node, npm, every dev dependency and your source code, just to serve a folder of static files.
Multi-stage builds
Use several FROM stages in one Dockerfile. Build in a heavy stage, then copy only the output into a tiny final stage. Everything else is thrown away.
# ---- Stage 1: build ----
FROM node:22-alpine AS build
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build # outputs /app/dist
# ---- Stage 2: serve ----
FROM nginx:1.27-alpine
COPY --from=build /app/dist /usr/share/nginx/html
EXPOSE 80The final image is just Nginx plus your built files: around 50 MB instead of 1 GB+.
Compiled languages: even smaller
Go compiles to a single static binary, so the final stage can be almost empty:
FROM golang:1.23 AS build
WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 go build -ldflags="-s -w" -o /bin/server ./cmd/server
FROM gcr.io/distroless/static-debian12
COPY --from=build /bin/server /server
USER nonroot
ENTRYPOINT ["/server"]Distroless images contain only your app and its runtime: no shell and no package manager. That means very little for an attacker to use. The result is often under 20 MB.
Node.js API: production dependencies only
FROM node:22-alpine AS deps
WORKDIR /app
COPY package*.json ./
RUN npm ci --omit=dev
FROM node:22-alpine AS build
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build
FROM node:22-alpine
WORKDIR /app
ENV NODE_ENV=production
COPY --from=deps /app/node_modules ./node_modules
COPY --from=build /app/dist ./dist
COPY package.json .
USER node
CMD ["node", "dist/server.js"]Choosing a base image
| Base | Size | Notes |
|---|---|---|
ubuntu / debian |
~75–120 MB | Familiar, lots of tools |
*-slim |
~30–80 MB | Debian without extras; good default |
*-alpine |
~5–50 MB | Tiny; uses musl libc (occasional compatibility issues) |
distroless |
~2–20 MB | No shell; most secure; harder to debug |
scratch |
0 MB | Completely empty; for static binaries only |
More size and speed tricks
# Combine commands and clean up in the SAME layer
RUN apt-get update \
&& apt-get install -y --no-install-recommends curl \
&& rm -rf /var/lib/apt/lists/*
# Python: don't keep pip's cache
RUN pip install --no-cache-dir -r requirements.txt- Deleting files in a later layer doesn't shrink the image. The bytes still live in the earlier layer.
- Use
.dockerignoreaggressively. - Use BuildKit cache mounts for faster rebuilds:
RUN --mount=type=cache,target=/root/.npm npm ciMeasure it
docker images my-app
docker history my-app:latest # which layers are big?
# Great tool for exploring layers:
docker run --rm -it -v /var/run/docker.sock:/var/run/docker.sock wagoodman/dive my-app:latestPractice
Take the Node app from the Dockerfile lesson (or any React/Vite app) and:
- Build it single-stage and note the size.
- Rewrite it as a multi-stage build and compare.
- Try
-slimvs-alpinebases and record the results in a table.
1.What gets shipped in the final image of a multi-stage build?
2.You run RUN rm -rf /big-folder in a later step. Does the image shrink?