A team shipped a 200-line service. The Docker image was 1.2 GB. Inside it sat a full Debian install, three package managers, a C compiler, and a .git folder from 2023. Deploying a hotfix took four minutes, and most of that was just waiting for the image to pull.
Nobody sets out to build a 1.2 GB image. It usually happens because someone wrote the Dockerfile for their laptop, it worked, and nobody looked at it again. I've done this myself more than once. So let's look at what's actually taking up space, rewrite the Dockerfile, and see how much we can cut.
1. Find Out What's Eating Your Disk
Don't guess. Start by looking at the image itself. Two tools are handy here:
-
docker history <image>lists every layer and how big it is. It's quick and rough, but it usually points you straight at the fat layer. -
divelets you browse the layers interactively and see which files each one added. It's the fastest way to find a stray.gitfolder, a package manager cache, or build leftovers.
In my experience, the biggest layers in a bloated image are almost always the package installs and the COPY . . that pulled in the whole project. The application code itself is usually tiny by comparison.
2. The Dockerfile That Started It All
Here's a realistic example for a Go service:
FROM ubuntu:22.04
RUN apt-get update && apt-get install -y golang git gcc python3 python3-pip
COPY . /app
WORKDIR /app
RUN go build -o server .
CMD ["./server"]
There are three problems here:
- The base image is huge. A full Ubuntu install is way more than a small service needs.
- Build tools end up in production. The compiler, Git, and package managers have no reason to be in a runtime image.
-
COPY . /appgrabs everything, including.git, local configs, and that.envfile with the production database password. Yes, that happens more often than anyone admits.
3. Split the Build from the Runtime
A multi-stage build uses two stages. The first one compiles the code. The second one starts from a clean, minimal image and copies in only the finished binary. The compiler never makes it to production.
# Stage 1: build
FROM golang:1.25 AS builder
WORKDIR /src
# Copy dependency files first so this layer stays cached
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 GOOS=linux go build -o /out/server .
# Stage 2: runtime
FROM gcr.io/distroless/static-debian12:nonroot
COPY --from=builder /out/server /server
EXPOSE 8080
ENTRYPOINT ["/server"]
A few things worth noticing:
-
Order matters. Copying
go.modandgo.sumbefore the source means Docker only re-downloads dependencies when they actually change. Without this, every typo fix triggers a full dependency download. -
CGO_ENABLED=0gives you a statically linked binary that doesn't need system libraries. That's what makes a tiny base image possible. -
:nonrootruns the process without root. It's not a silver bullet, but it's a free security win.
Next, add a .dockerignore so junk never reaches the build context in the first place:
.git
.gitignore
Dockerfile
.dockerignore
*.md
bin/
tmp/
.env
I'd argue .dockerignore matters more for security than for size. Keeping secrets out of the image is a much bigger deal than saving a few megabytes.
A note for Python folks: the same idea applies. Install your dependencies in a build stage with a full Python image, then copy only the result (a virtual environment or a dedicated site-packages directory) into a slim runtime image. Add --no-cache-dir to pip install so pip's cache doesn't get baked into a layer. And chain related RUN commands together, because deleting a file in a later layer doesn't remove it from the earlier layer. It's still in the image, just hidden.
4. Choosing the Base Image
The base of your final stage makes the biggest difference to size. Here's how I'd think about it:
| Base image | When it fits | Things to know |
|---|---|---|
scratch |
A static binary that needs nothing else | Completely empty. No shell, no CA certificates. |
distroless/static |
A static binary that needs CA certs and timezone data | My default for Go and Rust services. |
distroless/base |
A dynamically linked binary that needs glibc | Good for C/C++, or Python with native extensions. |
alpine |
A general-purpose small base | Uses musl instead of glibc. That can cause subtle bugs, and some Python wheels end up compiling from source. |
Alpine gets recommended constantly, but it isn't always the right answer. Distroless or slim images usually get you most of the savings without swapping out your C library, which saves you from a whole category of weird bugs.
On security: fewer packages means fewer CVEs to track and patch. Distroless images also ship without a shell or a package manager, so if an attacker gets in, they have far fewer tools to work with. The downside is that you can't just docker exec -it container sh to poke around when something breaks. Google publishes a :debug variant for that, but please don't ship it to production.
5. Before and After
These numbers are illustrative, based on a typical Go service. Your results will vary with your app and your dependencies, so measure your own images with docker images and docker history before you believe any table, including mine.
| Metric | Before (Ubuntu + tools) | After (multi-stage + distroless) |
|---|---|---|
| Image size | ~1.2 GB | ~15–20 MB |
| Base layer | ~80 MB Ubuntu + tools | ~2 MB distroless/static
|
| Build time (no cache) | several minutes | under a minute, roughly |
| Pull time at 100 Mb/s | ~1.5 minutes before overhead | a second or two |
| Packages in the image | hundreds | almost none (just CA certs and tzdata) |
The pull math: 1.2 GB is about 9,600 megabits. At 100 Mb/s, that's around 96 seconds before registry overhead, so the real number is a bit higher. A 15 MB image transfers almost instantly. That gap is the difference between a hotfix that sits in a queue for four minutes and one that's live before your coffee gets cold.
For a statically compiled binary, 10 to 20 MB is a realistic target.
The Short Version
- Measure before you optimize.
docker historyanddivewill show you the real culprits. - Separate build from runtime with a multi-stage build.
- Use
.dockerignoreto keep.git, secrets, and temp files out of the image. - Pick the smallest base your app can actually run on.
- Run as non-root whenever you can.
You end up with a smaller image, faster deploys, and less to defend. And the team from the intro gets its four minutes back on every hotfix.
Top comments (0)