Image size is not about beauty but about delivery speed and attack radius. Every extra layer is extra megabytes in the registry, extra minutes in CI and extra surface for vulnerabilities: the scanner checks what you need, plus the compiler, the caches, and the test utilities that will never run in production.
The reference multi-stage build
The idea: build and runtime are different worlds, and only the result should travel to production. For Go this looks the most radical — a whole toolchain collapses into a single binary:
FROM golang:1.22 AS build
WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 go build -trimpath -ldflags="-s -w" -o /app .
FROM gcr.io/distroless/static:nonroot
COPY --from=build /app /app
USER nonroot
ENTRYPOINT ["/app"]
What saves space and nerves here:
- Layer cache.
go.mod/go.sumare copied first and dependencies downloaded — a code edit no longer rebuilds them. -trimpath -ldflags="-s -w"— minus symbols and debug info, up to 30% off the binary.distroless— a runtime without a shell, without a package manager: only the application code fits, and "hop into the container to tinker" becomes impossible — a Bad Thing for debugging, a Good Thing for security.
Typical base image families
| Base image | Size | Has a shell | When to use |
|---|---|---|---|
ubuntu:24.04 | ~78 MB | yes | when you need system utilities (rarest of all) |
alpine:3.19 | ~7 MB | yes (busybox) | scripts and utilities; for Go/grpc — verify musl compatibility |
debian:bookworm-slim | ~74 MB | yes | glibc compatibility at a small size: the whole package world |
distroless | 2–25 MB | no | production for static binaries |
The industrial practice for Python is multi-stage with python:slim: wheels are built in a separate stage, production gets the interpreter and site-packages without the build chain.
Rules that stick
- Multi-stage by default. Compilers and caches don't ride to production — enforced not as an opinion but as a CI template with a size check.
.dockerignorenext to the Dockerfile. Without it everything rides into the context, including.git: the context grows for minutes — I've seen it: a 600-megabyte context to build a 10-megabyte image.
.git
.env*
target/
node_modules/
*.log
tests/
- Tags by digest.
latestis not a version but a hope: deploying "the same image" withlatestdoesn't reproduce after a rebuild. The minimal honest tag is git SHA + release version. - The "not root" mark.
USER nonrootin the Dockerfile: least privilege starts here, not in Kubernetes. - One process — one image. Supervisord inside a container takes us back to "everything in one VM", only harder to debug.
The check that becomes routine
# image and layer sizes
docker history app:2026.11.3
dive app:2026.11.3 # interactive: what's in each layer
# what actually runs inside
docker exec app ps aux # won't work in distroless - which is the check
An image that weighs as much as an OS is almost always a sign that nobody has looked at its build with their own eyes. A good norm: weigh the image in CI and stop the pipeline if it grows without an explainable reason.
Scanners and vulnerabilities: what to do with findings
trivy image app:2026.11.3 --severity HIGH,CRITICAL --exit-code 1
| Finding | What it means | Action |
|---|---|---|
| CVE in the base image | a vulnerability in OS packages | bump the base image (pinned by digest — no "magic") and rebuild |
| CVE in application dependencies | libraries of your language | update the lock file; the scanner won't see them in distroless — scan the source stage too |
| Critical with an exploit, but the package is unused | scanner noise | mark an exception with a comment and an expiry date, not "MUTE forever" |
The "zero findings in the image" rule is a path to CI burnout. The working alternative: fail only on HIGH/CRITICAL with a public exploit, everything else — a report in the MR.
Python and Node: two frequent exceptions
For interpreted stacks multi-stage looks different — and still saves:
# Python: wheels in the builder, runtime without the build chain
FROM python:3.12 AS build
COPY requirements.txt .
RUN pip wheel --no-cache-dir --wheel-dir /wheels -r requirements.txt
FROM python:3.12-slim
COPY --from=build /wheels /wheels
RUN pip install --no-cache-dir /wheels/* && rm -rf /wheels
USER 1000
# Node: without dev dependencies and build sources
FROM node:22 AS build
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . . && npm run build
FROM node:22-slim
WORKDIR /app
COPY --from=build /app/dist ./dist
COPY --from=build /app/node_modules ./node_modules # prod deps only: npm ci --omit=dev
USER 1000
Typical savings for a Python app: a 1.2 GB naive image → 150–200 MB slim; for Node: 1.5 GB → 180 MB. Not "pretty" but "delivery got faster, the scanner stopped finding compilers".
Layer cache in CI: why builds don't start "from scratch"
- The order of COPY decides: lock files first, code later — a README edit no longer invalidates the "download dependencies" layer.
- BuildKit cache mounts for package managers (
--mount=type=cache,target=/root/.cache/pip): layers don't grow, the cache lives on. - CI with a registry cache (cache-to/cache-from type=registry): builds on runners without local cache still land in warm layers.