Smaller Docker images with multi-stage builds
Most Docker images are bigger than they need to be. The usual reason is simple: the Dockerfile installs the compiler, the test tools, and the package cache, builds the application, and then ships all of it. The final image carries the entire workshop that built it. That costs time on every pull, disk on every node, and attack surface on every container.
A multi-stage build fixes this by splitting the Dockerfile into stages. The first stage is the workshop: it has the compiler and the dependencies, and it produces the application binary. The second stage is the shipping box: it starts from a small base image and copies only the finished binary into it. Nothing else travels. A Go service that needed a full Go toolchain to build can ship in a minimal image with one binary. A Python application can build its wheels in one stage and install only the wheels in the next, leaving pip and the build headers behind.
The pattern looks like this in practice. Stage one is named build, and it runs the compile step. Stage two starts from a slim base, copies the artifact out of stage one with COPY --from=build, and sets the entrypoint. The two stages share nothing except the artifact you choose to copy. If you need a shell for debugging, use a debug variant of the base image rather than stuffing tools into production. On my own services I build both: a slim image for production and a debug image with a shell for the staging environment, from the same Dockerfile.
Order matters for speed as much as size. Docker caches each layer, and a cache miss rebuilds everything below it. So copy the dependency files first and install dependencies before copying the source code. That way a one-line code change reuses the cached dependency layer instead of reinstalling the world. A .dockerignore file helps too: keep .git, test data, and local configuration out of the build context entirely, so the daemon never sends them in the first place.
Pin your base images. python:3.13-slim today is not the same image it was last month, and an unpinned tag can change under you without a word. A digest pin makes the build reproducible: the same input gives the same image, and an upgrade is a deliberate commit, not an accident. The same goes for the packages you install inside the image. Reproducible builds are what let you answer the question “what changed?” with a commit hash instead of a shrug.
Finally, measure. Run docker images before and after, and look at the number. A service that went from hundreds of megabytes to tens of megabytes pulls faster on every node, starts faster on every restart, and presents a smaller target. Small images are not a vanity metric. They are what makes the rest of the pipeline — the registry, the pull, the rollout — cheap enough to do often.