Sub topic 8
Finding wasted space across layers
This chapter assumes you can build and run images, read a simple Dockerfile, and use a Linux command line. You should also know that docker history lists layers and that dive shows a file tree per layer. Here you will learn what wasted space is, why it appears, and how to find it.
What wasted space is
An image layer is a read-only set of filesystem changes. A layer never shrinks the layers below it. If a later layer deletes a file, the image records a "whiteout" marker, and the file's bytes stay in the earlier layer. The container no longer sees the file, but you still download and store it.
Overwritten files behave the same way. If layer 3 rewrites a file that layer 2 created, the image keeps both copies. Dive counts these hidden bytes as wasted space.
A wasteful example
This Dockerfile has two problems.
FROM debian:bookworm-slim
RUN apt-get update
RUN apt-get install -y curl
COPY data.tar /tmp/data.tar
RUN tar -xf /tmp/data.tar -C /opt
RUN rm /tmp/data.tar- The
apt-get updatelayer stores the package lists in/var/lib/apt/lists, and nothing ever removes them. COPYstoresdata.tarin its own layer. The finalrmonly hides the file. The tar bytes remain in theCOPYlayer.
Build it and check the layer sizes:
docker build -t demo:wasteful .
docker history demo:wastefulIn the output, the COPY row is as large as data.tar. The rm row shows 0B. That zero is the clue: a delete adds almost nothing, and it frees nothing either. docker history shows sizes but not which files are wasted. For that you need dive.
Finding the waste in dive
Open the image in dive:
dive demo:wastefulThe left pane lists layers. The right pane shows the file tree for the selected layer. Use these keys:
Tabswitches between the layer pane and the file tree.Ctrl+UandCtrl+Atoggle hiding of unmodified and added files, which isolates what a layer changed.Ctrl+Ltoggles the layer view between the selected layer's own changes and the cumulative state up to that layer.Ctrl+Spacecollapses or expands directories.
The lower left shows image details: total image size, the wasted space estimate, and the efficiency score covered in the next chapter.
To find waste, work through these steps:
- Select the
RUN rm /tmp/data.tarlayer and look for removed entries. Dive marks removed files in red. - Select the
COPYlayer. Under/tmp, you will seedata.tarstill present at full size. - Compare the two. A file that is added in one layer and removed in a later one is wasted space.
- Check the
apt-get updatelayer. Expand/var/lib/apt/liststo see files that no later step uses.
How the waste adds up
The image carries data.tar once in the COPY layer, plus its extracted contents in /opt. The rm layer does not reduce either. The apt lists add more.
Why the fix works
The improved Dockerfile removes the cause instead of hiding the result:
FROM debian:bookworm-slim
RUN apt-get update \
&& apt-get install -y --no-install-recommends curl \
&& rm -rf /var/lib/apt/lists/*
RUN --mount=type=bind,source=data.tar,target=/tmp/data.tar \
tar -xf /tmp/data.tar -C /opt- The apt lists are created and deleted in the same
RUN, so no layer ever stores them. - The bind mount makes
data.tarvisible only while thatRUNexecutes. It is never written into a layer. This needs BuildKit, the default builder in current Docker.
Rebuild as demo:improved and open it in dive. The wasted space figure should drop sharply, and the layer list is shorter.
Exercise
A Dockerfile has RUN wget https://example.com/big.zip followed by RUN unzip big.zip && rm big.zip. Where does big.zip still take up space, and how would you confirm it in dive?
Show answer
The zip stays in the layer created by the wget step, because the later rm only adds a whiteout in a newer layer. In dive, select the wget layer and find big.zip in the file tree at full size. Then select the unzip layer and see it marked as removed. Fix it by running wget, unzip, and rm in one RUN, or by using a multi-stage build.