Learn to Code
All tracks
Learn to Code

Free lessons from scratch to pro — every language, databases and DevOps.

AboutContactPrivacy PolicyTerms of Use

Your progress is saved in this browser. © 2026 Learn to Code

DK
Docker
0/10 completed
Beginner
Why containers?Install Docker and run your first containerImages vs containers
Intermediate
Writing a DockerfileVolumes and persistent dataNetworking and ports
Advanced
Docker ComposeMulti-stage builds and small images
Pro
Security and production best practicesRegistries and CI/CD
Tracks / Docker / Advanced

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

dockerfile
# ✗ 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.

dockerfile
# ---- 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 80

The 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:

dockerfile
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

dockerfile
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

dockerfile
# 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 .dockerignore aggressively.
  • Use BuildKit cache mounts for faster rebuilds:
dockerfile
RUN --mount=type=cache,target=/root/.npm npm ci

Measure it

bash
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:latest

Practice

Take the Node app from the Dockerfile lesson (or any React/Vite app) and:

  1. Build it single-stage and note the size.
  2. Rewrite it as a multi-stage build and compare.
  3. Try -slim vs -alpine bases and record the results in a table.
✎ Check your understanding

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?

← Docker Compose