Sub topic 2
Why smaller images: size and attack surface
This chapter covers why a container image should hold as little as possible. It assumes you can write a basic Dockerfile, run docker build and docker run, and build a program in a compiled language such as Go. It also assumes you know that Linux programs can load shared libraries.
What an image carries
A base image such as debian or ubuntu brings a lot with it. It includes a shell, a package manager, core utilities such as ls, cat, and curl, and many libraries. Your compiled program may use only a small part of this. The rest ships with every copy of the image anyway.
You can see the difference by building the same service on different bases. The numbers below are approximate and vary by version.
| Base image | Typical size | Contains a shell | Contains a package manager |
|---|---|---|---|
| `golang:1.22` (full toolchain) | about 800 MB | yes | yes |
| `debian:12-slim` | about 75 MB | yes | yes |
| `gcr.io/distroless/static-debian12` | about 2 MB | no | no |
| `scratch` | 0 bytes | no | no |
A static Go service of a few megabytes on scratch gives an image of a few megabytes in total.
Size has direct costs
Smaller images are cheaper to run in several ways:
- They pull faster, so new nodes and rescheduled pods start sooner.
- They use less registry storage and less network transfer.
- They are quicker to push from CI.
These gains are real, but they are the minor reason. The main reason is security.
Attack surface
The attack surface of an image is everything an attacker could use once they get code running inside the container. Each extra file adds to it in two ways.
- Known vulnerabilities. Every installed package can have published security flaws (CVEs). Scanners report them whether or not your program uses the package. A full Debian image often shows dozens of findings. A static binary on scratch has no packages to report.
- Tools for the attacker. Suppose an attacker exploits a bug in your service and can run commands. With a shell,
curl, and a package manager, they can download more tools, explore the network, and move around. Without those, they have far fewer options.
The chapter's diagram below shows how the layers shrink.
Limits of this approach
A small image reduces risk but does not remove it. Keep these points in mind:
- Your own code and its dependencies still ship. A flaw in your HTTP handler is exploitable on any base.
- A minimal image does not stop the first compromise. It limits what an attacker can do next.
- Smaller images are harder to debug, because the usual tools are gone. Later chapters cover how to handle that.
Image size alone is also a poor target. A 5 MB image that runs as root with broad permissions can be riskier than a 50 MB image that runs as an unprivileged user. Treat size as one signal and look at what the image contains.
Checking your own images
You can measure an image with standard commands:
docker build -t myapp:full -f Dockerfile.full .
docker images myapp
docker history myapp:fulldocker images shows the total size. docker history shows how much each layer adds, which helps you find where the weight comes from.
Exercise
A service runs on a full Debian image but uses only its own compiled binary. Give two reasons the extra packages in the image matter for security.
Show answer
Each installed package can carry known vulnerabilities that scanners report even if the program never uses it. A shell and tools such as curl also give an attacker who gains code execution more ways to download tools and move further.
Exercise
Why is image size not a complete measure of how safe an image is?
Show answer
Size says nothing about who the process runs as, what permissions it has, or what flaws are in your own code and dependencies. A small image running as root can be riskier than a larger image running as an unprivileged user.