Sub topic 3
Why size and attack surface matter
This chapter assumes you can build and run images, read a simple Dockerfile, and know that a package manager installs software and a shared library is code loaded by programs at run time. It explains why base image choice is partly a security decision and partly an operations decision.
What size costs you
Image size is the sum of its layers. The base image supplies the first layers, so it sets a floor that your application cannot go below.
Larger images cost you in several ways:
- Pulls take longer on every new node, autoscaling event, and CI runner with a cold cache.
- Registry storage and transfer grow with every tag you push.
- Startup is slower when a node must download the image before it can run the container.
As a rough guide, a full Debian-based python:3.12 image is on the order of a gigabyte. The -slim variant is on the order of a hundred megabytes. Exact numbers vary by tag and CPU architecture, so measure the images you use.
What attack surface means here
Attack surface is everything in the running container that an attacker could use or that could contain a flaw. In an image, that includes:
- Installed packages. Each one can have known vulnerabilities (CVEs).
- Shells and utilities such as
sh,curl, orwget. After a compromise, these help an attacker explore, download tools, and move sideways. - A package manager. It lets an attacker install more tools at run time.
- Extra libraries your app never loads. They still show up in scanner reports.
A smaller image usually has fewer of these. Fewer packages means fewer CVEs to triage. Fewer tools means an attacker who gets code execution in your app has less to work with.
Size is a proxy, not the goal
Size correlates with attack surface, but it is not the same thing. Two cautions apply.
First, a small image can still carry a vulnerable package. What matters is what is installed and whether it is patched. A tiny image that is never rebuilt gathers CVEs over time.
Second, many CVEs in a scan are not exploitable in your app. A flaw in a command-line tool you never run is noise, but it still costs triage time. Removing the tool removes the finding. This is one reason minimal images are attractive: they cut noise as well as risk.
Smaller also has a price. Alpine uses a different C library (musl), and distroless has no shell. Later chapters cover those trade-offs. Do not pick the smallest image by default. Pick the smallest one your application runs on reliably and that your team can debug.
Measuring your own images
Measure before you decide. The commands below list local image sizes and scan an image for known vulnerabilities with Docker Scout and Trivy.
docker image ls
docker scout cves myapp:latest
trivy image myapp:latestBuild your app on two candidate base images, tag them differently, and compare the output. Look at three things: total size, the count of vulnerabilities by severity, and whether the findings are in packages your app actually uses.
A practical reading of the numbers
Use these questions when you look at a scan result:
- Is the vulnerable package part of the application, or part of the base image?
- Does your app load it at run time?
- Is a fixed version available, and would a rebuild on a newer base pick it up?
- Could a different base image avoid the package entirely?
The last question is where base image choice pays off. Fixing a CVE means patching and rebuilding. Avoiding the package means there is nothing to patch.
Exercise
Your team's image is 900 MB and a scan reports 150 vulnerabilities, almost all in packages like perl and curl that your Python service never calls. Name two benefits of moving to a smaller base image, and one reason you should not choose purely on size.
Show answer
Benefits: faster pulls and lower storage cost, and fewer scanner findings and fewer tools available to an attacker after a compromise. Reason not to choose on size alone: a small image can still contain unpatched packages, and a very minimal one (musl libc in Alpine, no shell in distroless) can break native dependencies or make debugging harder. Choose the smallest image your app runs on reliably, and keep it rebuilt and patched.