Sub topic 7
Why deleting files in a later layer does not shrink the image
This chapter assumes you can read a Dockerfile, run docker build, and know that an image is a stack of read-only layers. You will see why a rm in a later step leaves the image size unchanged, and how to fix it.
How a delete is stored
Each RUN, COPY, and ADD instruction produces a layer. A layer records only the changes that step made to the filesystem. Earlier layers are never edited, because they are read-only and identified by their content digest.
When a later step runs rm big.tar.gz, the runtime cannot remove the file from the earlier layer. Instead, the new layer stores a marker, called a whiteout, that says the file is deleted. The union filesystem merges the layers and shows the file as gone. The bytes still sit in the older layer, and every pull and push of the image transfers them.
The problem in a Dockerfile
This Dockerfile downloads an archive, then deletes it in a separate step.
FROM debian:bookworm-slim
RUN apt-get update && apt-get install -y --no-install-recommends curl ca-certificates \
&& rm -rf /var/lib/apt/lists/*
# Two separate RUN steps: the archive stays stored in the first layer.
RUN curl -fsSLO https://example.com/big.tar.gz
RUN tar -xzf big.tar.gz -C /opt && rm big.tar.gzThe curl step creates a layer that contains the archive. The tar step creates another layer with the extracted files and a whiteout for the archive. The final image is larger than the sum of the extracted files alone.
The fix: create and delete in one step
Do the download, use, and cleanup inside a single RUN. The layer is then created after the cleanup, so the deleted files never enter it.
FROM debian:bookworm-slim
RUN apt-get update \
&& apt-get install -y --no-install-recommends curl ca-certificates \
&& curl -fsSLO https://example.com/big.tar.gz \
&& tar -xzf big.tar.gz -C /opt \
&& rm big.tar.gz \
&& rm -rf /var/lib/apt/lists/*The same rule applies to package indexes. Running apt-get update, the install, and rm -rf /var/lib/apt/lists/* in one RUN keeps the lists out of every layer. It also avoids a stale cached index, because update and install are cached together.
FROM debian:bookworm-slim
RUN apt-get update \
&& apt-get install -y --no-install-recommends curl \
&& rm -rf /var/lib/apt/lists/*Verify with docker history
Check the result instead of assuming it. Build the image and compare the size of each step.
docker build -t myapp:dev .
docker history myapp:devIn the broken version, the curl step shows the archive's size, and the rm step shows almost none. In the fixed version, no step carries the archive.
When one RUN is not enough
Merging everything into one RUN has a cost: any change re-runs the whole step, so it hurts cache reuse. Use it for short-lived files that you create and discard together. For build tools and source files, use a multi-stage build, which is covered in a later chapter.
| Situation | Approach |
|---|---|
| Temporary download or package index | Create and delete in the same `RUN` |
| Compilers and sources needed only to build | Multi-stage build |
| Files that should never be copied in | `.dockerignore` |
Exercise
A Dockerfile has RUN curl -O https://example.com/data.zip followed by RUN unzip data.zip -d /data && rm data.zip. The image is still as large as if the zip had not been deleted. Why, and how do you fix it?
Show answer
The first RUN stores data.zip in its own read-only layer. The second RUN only adds a whiteout marker, so the zip bytes remain in the earlier layer and in the image size. Fix it by downloading, unzipping, and removing the file in one RUN, so the zip is never written into any committed layer.