
Your cluster is not slow because of your code. It is slow because your image is 1.2 GB of Debian, build tools, dev dependencies, and a .git directory that nobody needs in production. Every extra megabyte is paid for three times: registry storage, pull bandwidth on every deploy and scale-out, and CVE scan time. Here is how an SMB team gets from 1.2 GB to under 150 MB in one afternoon — without touching the application.
The Real Cost of a Fat Image
Run the math on a modest fleet: 20 services, 3 tags each, 1 GB per image. That is ~60 GB of registry storage and, with daily deploys across zones, terabytes of layer pulls per month. A 1 GB image on a 1 Gbps link takes 60–90 seconds to pull on a cold node; a 200 MB image takes under 12. In Kubernetes, that gap is the difference between absorbing a traffic spike and paging someone — the new replica sits in Pending while the kubelet downloads layers. It is also the same cost spiral we broke down in cutting Kubernetes bills without sacrificing reliability.
Step 1: Measure Before You Cut
# Layer-by-layer view of where the weight lives
dive your-registry/app:latest
# Quick size + vulnerability snapshot (Docker Scout)
docker scout cves your-registry/app:latest
docker scout size your-registry/app:latest
Registry storage alone makes this worth an hour: at AWS ECR’s $0.10 per GB-month, a fleet of 60 stored tags of a 1 GB image costs ~$6/month per service before a single pull, and every cross-AZ layer transfer adds egress on top. Scan time behaves the same way — a 1.2 GB image gives the scanner roughly four times the packages to check, which slows the pipeline that is supposed to keep you safe. A typical dive report tells the whole story at a glance:
Current: 1.24 GB (wasted: ~890 MB)
├── node:20 1,006 MB base includes gcc, python, git
├── npm install 418 MB devDependencies shipped to prod
├── COPY . . 82 MB .git + docs + tests
└── npm run build 61 MB the only layer you actually need
The base image is 60–80% of the weight, dev dependencies another 15%, and the app itself a rounding error. A single apt-get install build-essential layer can be 250 MB you ship forever.
Step 2: Multi-Stage Builds That Actually Shrink Things
Build in one stage, run in another, and never let the toolchain touch the runtime image:
# syntax=docker/dockerfile:1
FROM node:22 AS build
WORKDIR /app
COPY package*.json ./
RUN npm ci --omit=dev && npm cache clean --force
COPY . .
RUN npm run build
FROM node:22-slim AS runtime
WORKDIR /app
COPY --from=build /app/node_modules ./node_modules
COPY --from=build /app/dist ./dist
USER node
EXPOSE 3000
CMD ["node", "dist/main.js"]
Pair it with a .dockerignore so the build context stops smuggling junk in:
.git
node_modules
dist
*.md
.dockerignore
Dockerfile
Lock your base images by digest, not by tag, and enforce hygiene in CI so nobody regresses:
- name: Lint Dockerfile
run: docker run --rm -i hadolint/hadolint < Dockerfile
- name: Scan image
run: docker scout cves --only-severity critical,high your-registry/app:${{ github.sha }}
Use BuildKit cache mounts (RUN --mount=type=cache,target=/root/.npm npm ci) so the lean --omit=dev install stays fast.
Step 3: Distroless and the Last 80%
node:22-slim still ships a shell and a package manager. A distroless base ships your runtime, its libraries, CA certs, and nothing else — typically 20–40 MB, with no shell for an attacker to land in. The pattern maps to every stack:
# Go: static build on distroless (often 90%+ smaller than a full golang base)
FROM golang:1.24 AS build
WORKDIR /src
COPY . .
RUN CGO_ENABLED=0 go build -o /out/app ./cmd/server
FROM gcr.io/distroless/static-debian12
COPY --from=build /out/app /app
USER nonroot:nonroot
ENTRYPOINT ["/app"]
One caveat: distroless breaks when an app needs dynamic linking, timezone data, or CA bundles the base does not ship. Fix that explicitly (COPY --from=build /usr/share/zoneinfo /usr/share/zoneinfo, or a one-line apt-get install -y --no-install-recommends tzdata in a slim stage) instead of falling back to full Debian. Keep a single debug variant (:debug with a shell) for incidents, and never promote it to production. Sign the final image and attach an SBOM so the slim artifact is also a verified one — the mechanics are in our SBOM and image signing guide, and the pipeline that enforces it fits the pattern in automating security in an SMB CI/CD pipeline.
The 15-Minute Maintenance Habit
- Weekly: rebuild base images (
docker buildx build --pull) and reviewdocker scout cvesoutput. - Monthly: re-run
diveon the three biggest services; delete any layer that grew without a reason. - Quarterly: audit registry storage per service and set pull-through caches so cross-AZ egress does not silently grow.
Track one number on a dashboard: median image size per service. When it trends up, you know exactly which PR to blame. Image diet is boring, compounding work — exactly what a lean team should automate instead of doing by hand. If you want a tailored pass over your registries and pipelines, book a free 30-minute DevOps consultation and we’ll find your ten biggest layers together.