Why slim images are worth the effort
Container images have a habit of growing. A base image here, a build tool there, and before long you are shipping a 1.2GB artefact that takes an age to pull and scans like a telephone directory of vulnerabilities. Slimming images is not just vanity. Smaller images pull faster, start quicker, consume less registry storage and present a smaller attack surface. The trick is doing it without turning every production incident into a forensic nightmare.
Scratch: the blank canvas
Scratch is the empty image. It contains nothing at all, which makes it the purest starting point for statically compiled binaries. Go and Rust are natural fits: build a static binary, copy it into a scratch image, and you end up with an image measured in single-digit megabytes. There is no shell, no package manager, no libc, and nothing for an attacker to curl up inside.
That purity comes with a catch. If the binary fails to start, you cannot exec into the container to poke around. There is no ls, no cat, no sh. You are debugging by logs and exit codes alone. Scratch is brilliant for small, well-understood services, but it demands discipline: thorough health checks, structured logging and a reliable build pipeline. For a closer look, TheVarsityJacket.shop covers this in more detail.
Distroless: minimal but not empty
Distroless images sit a step up from scratch. They include the runtime essentials your application needs, such as CA certificates, timezone data and the language runtime, but strip out package managers, shells and the usual grab bag of Unix utilities. Google's distroless images are the best-known example, though other projects follow the same philosophy.
In practice, distroless is a sweet spot for many JVM, Node and Python services. You get smaller images and a reduced vulnerability surface without building a static binary. You still lose the interactive shell, so the same debugging constraints apply. Some distroless variants offer a debug tag that adds busybox for troubleshooting, which is handy during development but should not reach production.
The debugging dilemma
The moment you remove a shell, someone will need one. A pod crash-loops at 3am, and the usual kubectl exec into a shell is met with an exec format error or a missing binary. This is the point where teams either give up on slim images or learn a few better patterns. The good news is that you do not need a shell inside the container to debug it.
- Use ephemeral debug containers: modern Kubernetes lets you attach a separate debugging container to a running pod. You get your tools without bloating the application image.
- Copy the binary out: if you need to inspect a scratch image, create a temporary debug image from the same binary with a shell added. Reproduce the failure there.
- Lean on structured logs and metrics: if you cannot exec, your observability has to carry the weight. Ship logs to a central store and make sure exit codes are meaningful.
- Keep a debug variant in the registry: build a slim production image and a fatter debug image from the same source. Pull the debug one only when needed.
- Test the slim image in CI: run your integration tests against the exact image you deploy, not a fatter build variant. Otherwise you discover missing certificates in production.
Practical patterns that keep both sides happy
A pragmatic approach is to treat image slimming as a spectrum rather than a binary choice. Start with a distroless base for most services, and reserve scratch for small, static binaries where you are confident in the operational story. Use multi-stage builds to keep compilers and build caches out of the final image. Pin your base image digests, and rebuild regularly so security patches actually land.
It also helps to document the operational trade-offs. Write down how to debug the service, which debug image to use and what logs are available. A runbook that says 'exec into the pod' is useless when the pod has no shell. The teams that succeed with slim images are the ones that invest in observability and clear procedures before they need them.
Finding your balance
Slim images are not about purity for its own sake. They are about reducing waste, risk and drag. Scratch and distroless are excellent tools, but they change how you operate. If you pair them with ephemeral debug containers, solid logging and a debug build in reserve, you can enjoy the benefits without dreading the next incident. The goal is not the smallest possible image; it is the smallest image you can confidently run and repair.


Leave a comment