Sub topic 9
Understanding the efficiency score
This chapter assumes you can build images, read a simple Dockerfile, and use the dive interface from the earlier chapters. It explains the number dive shows in the bottom-left panel and what it does and does not tell you.
What the score measures
Dive reports three values for the image you open:
- Image size: the total size of all layers.
- Wasted space: bytes that sit in a lower layer but are hidden or replaced by a higher layer, so they add to the image size without appearing in the final filesystem.
- Efficiency score: a percentage that compares the useful bytes with the bytes stored.
Wasted space comes from the cases covered in the previous chapter:
- A file is deleted in a later layer. The original bytes stay in the earlier layer.
- A file is overwritten in a later layer. The older version stays in the earlier layer.
- A file is modified, for example by
chmodorchown, so a whole new copy is stored in the new layer.
Dive builds the score by walking the layers from bottom to top and tracking each path. For every path, it counts the bytes stored in all layers that contain it. Only the version in the topmost layer is useful. The other copies count as wasted. A path that was removed counts as wasted in every layer that holds it.
Dive's own description: the score is a rough guide to the share of the image that is not wasted. It weights files by size, so one large deleted file lowers the score far more than many tiny ones.
A worked example
Build the wasteful image from earlier, then open it in dive:
docker build -t demo:wasteful .
dive demo:wastefulThe Dockerfile used was:
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.tarTwo things drive the waste here. The COPY layer stores data.tar, and the later rm only hides it. The apt package lists, created by apt-get update, remain in the image.
Now compare it with the improved version:
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 /optThe apt lists are removed in the same layer that created them, so no layer stores them. The bind mount makes data.tar available only during that RUN step, so it is never written to a layer. Rebuild, open the new image in dive, and compare the wasted space and the score. The wasted-space figure should drop sharply, and the score should rise toward 100%.
Reading the score with care
A high score means little wasted space. It does not mean a small image. Consider two cases:
| Image | Size | Wasted | Score |
|---|---|---|---|
| A: large base, no deletions | 1.2 GB | 0 MB | 100% |
| B: small base, one deleted file | 80 MB | 30 MB | about 62% |
Image A scores higher but is fifteen times larger. The score only tells you about duplication across layers. It cannot tell you that your base image is too big, or that you installed packages you do not need.
Use the score together with the other numbers:
- Check the total image size first. Decide whether it is reasonable for what the image does.
- Check wasted space in bytes. This tells you how much you could recover.
- Use the score as a ratio for tracking change over time.
Common mistakes
- Chasing 100%. Some waste is harmless. A package manager may rewrite a small database file in several layers. Fixing it can cost more in readability than it saves in bytes.
- Ignoring the score for multi-stage builds. Dive analyses only the final image. Files left behind in earlier build stages never enter it, so they do not appear as waste. A good score does not mean your build stages are lean.
- Comparing scores across unlike images. A score depends on layer count and file sizes. Compare the same image before and after a change.
- Trusting the score without looking at the layers. Open the layer details and the wasted-space view to find which files cause the loss.
Exercise
An image is 500 MB and dive reports an efficiency score of 99%. A colleague says it is already well optimised. What is wrong with that conclusion, and what would you check next?
Show answer
The score measures only waste caused by files that are deleted, overwritten, or copied again across layers. A 99% score can still belong to a large image, because the base image or installed packages may be bigger than needed. Check the total size, the size of each layer with docker history or the dive layer view, and whether a smaller base image or a multi-stage build would remove unneeded files.
Exercise
In the wasteful Dockerfile, which two sources of waste lower the score, and which change in the improved Dockerfile fixes each?
Show answer
First, data.tar stays in the COPY layer even though a later RUN deletes it. The improved file uses a BuildKit bind mount so the tar is never stored in a layer. Second, the apt lists stay in the image after the install. The improved file removes /var/lib/apt/lists/* in the same RUN instruction that created them.