Sub topic 5
Why images get large
This chapter explains where image size comes from, so you can decide what to remove. It assumes you can run containers with docker run, read a simple Dockerfile, and know that images are built from layers. Later chapters cover base image choice and multi-stage builds in detail.
Size is the sum of layers
An image is a stack of layers, and its size is the sum of those layers. Each RUN, COPY and ADD instruction that changes the filesystem adds a layer. Layers are immutable. Once a layer exists, later layers can hide its files but cannot shrink it.
The main sources of size
Most oversized images come from five causes.
- A large base image. A full Debian-based image can start at hundreds of megabytes before your code is added.
- Build tools left in the image. Compilers, headers and package managers are needed to build the app but not to run it.
- Development dependencies. Test frameworks, linters and type checkers get installed along with runtime packages.
- Package manager caches. Files such as
aptlists and downloaded archives stay in the layer that created them. - Unneeded files copied from the build context. Without a
.dockerignore,COPY . .can bring in.git, localnode_modules, logs and test data.
Deleting files in a later layer does not help
A common mistake is to install something in one RUN and remove it in the next.
FROM debian:bookworm-slim
RUN apt-get update && apt-get install -y build-essential
RUN apt-get remove -y build-essentialThe second layer records that the files were deleted, but the first layer still holds them. The image stays large. The cleanup has to happen in the same instruction that created the files.
FROM debian:bookworm-slim
RUN apt-get update \
&& apt-get install -y --no-install-recommends build-essential \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*This fixes the cache problem. It does not fix the larger one: if the final image needs only the compiled result, the compiler should not be in it at all. That is the problem multi-stage builds solve.
Seeing where the size is
Compare the size of an image's layers before changing anything.
docker image ls myapp
docker history myapp:latestLook for the layers with the largest SIZE values. A typical pattern is one very large layer from a package install or from COPY . .. Chapter 6 covers these commands in more detail.
Build-time and runtime needs differ
The table shows why one image should not do both jobs.
| Item | Needed to build | Needed to run |
|---|---|---|
| Compiler or bundler | Yes | Usually no |
| Test and lint packages | Sometimes | No |
| Package manager cache | No | No |
| Production dependencies | Yes | Yes |
| Compiled output or app source | Produced | Yes |
Everything marked "No" in the last column is a candidate to leave out of the final image. A single-stage Dockerfile cannot leave them out, because the same layers serve both purposes.
Exercise
A Dockerfile has RUN apt-get install -y gcc followed by a separate RUN apt-get remove -y gcc. The final image is still large. Why, and what are two ways to fix it?
Show answer
The first RUN creates a layer that contains gcc, and layers are immutable. The second RUN only adds a layer that marks the files as deleted, so the gcc data is still stored and pulled. Fix it by doing the install, use and cleanup in a single RUN instruction, so the files never persist in a layer. Better still, compile in a separate build stage and copy only the result into the final image.