ShrinkRay All Articles
Developer Productivity

Your Containers Are Packing Extra Baggage: A Layer-by-Layer Docker Audit

By ShrinkRay Developer Productivity
Your Containers Are Packing Extra Baggage: A Layer-by-Layer Docker Audit

Photo by Cosmin Andrei Buzamat on Unsplash

Your Docker image is not just a delivery vehicle. It is a copy of everything you decided to keep, plus a lot of things you forgot to throw away. A compiler here, a package cache there, a test fixture directory that nobody meant to ship. The result is a container that takes longer to push, longer to pull, and longer to scan. In late 2026, with CI minutes and registry egress still showing up on real invoices, image size is not a vanity metric. It is an operational one.

Look at the layers before you cut

Start with evidence. Run docker images to see the damage, then docker history --no-trunc your-image:tag. The history view lists each layer and the command that created it. Sizes can be a little noisy because metadata and whiteouts are weird, but the big offenders stand out fast. For a deeper view, run dive your-image:tag or use docker image inspect to check compressed size. The question is not which layer is technically largest. It is which layer contains something the running container does not need.

The usual suspects

Package manager caches are the classic. An apt-get update layer writes package lists to /var/lib/apt/lists. If you clean them up in a later RUN instruction, the final filesystem looks tidy, but the earlier layer still carries the bytes. Same story with pip wheels, npm tarballs, and Go module caches. Build tools are next: compilers, headers, test runners, and debug utilities that were needed during the build and are dead weight at runtime. Then there is the build context problem. A missing or lazy .dockerignore can copy .git, node_modules, local virtual environments, screenshots, and old build output into the image. That is not a layer problem. That is a packing problem.

Multi-stage builds are the baseline

If you are still compiling inside the same image you deploy, fix that first. A multi-stage build lets the builder stage keep the mess and the runtime stage copy only the artifact. For a Go service, that might mean a static binary copied into a distroless or scratch image. For a Node app, it often means a build stage that installs all dependencies and a runtime stage that installs production dependencies only. For Python, it can mean wheels built in one stage and installed into a slim runtime. The rule is simple: the final stage should contain the runtime, the app, and the libraries the app actually imports. Not the toolchain that produced it.

Choose a base you can defend

Base image choice is a trade-off, not a contest. Alpine is small, but musl libc can bite you if a native dependency expects glibc. Debian slim images are a reasonable middle ground for many teams. Distroless images remove the shell and package manager, which is great for security and terrible for debugging if you have not planned for it. Do not pick a base because it wins a size screenshot. Pick one that matches your runtime needs, pin it by digest, and have a plan for updates. A 30MB base that breaks your native module is not cheaper than a 90MB base that works.

Clean up in the same layer

Docker layers are immutable. Deleting a file in a later instruction does not remove it from the earlier layer. Chain the install and cleanup in one RUN command when you can. For Debian-based images, that means apt-get update && apt-get install -y --no-install-recommends ... && rm -rf /var/lib/apt/lists/*. For Python, use pip install --no-cache-dir. For Node, use npm ci --omit=dev in the runtime stage and avoid copying the cache. BuildKit cache mounts can help here, but they are a build-speed tool, not an excuse to ship the cache.

Set a budget and make it visible

Pick a size budget per image class. A small API service might target under 250MB. A worker with a heavier runtime might target under 400MB. A static frontend behind nginx should be much smaller. The exact number matters less than the habit. Add a CI check that reads the image size and compares it to the budget and to the main branch. Fail on a hard ceiling. Warn on a percentage jump. Print the top layers when a check fails so the next person knows where to look. Then review the budget quarterly. Containers should get leaner as the app matures, not heavier by default.

The goal is not the smallest possible image. It is the smallest image that still does the job reliably. Trim the caches, separate build from runtime, pick a base with intent, and put a number on the wall. Your CI pipeline and your registry bill will both notice.