Sub topic 12
Putting it together: analyse, fix, verify
This chapter joins the earlier steps into one routine. It assumes you can build images, read a simple Dockerfile, and use a Linux shell. It also assumes docker history and dive are installed and that you have met the idea of image layers.
The loop
Treat size work as a loop with a measurement at each end. Without a baseline you cannot tell whether a change helped.
Step 1: Build a baseline
Use the wasteful Dockerfile from earlier in the tutorial.
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.tarBuild and tag it, then record the total size.
docker build -t demo:wasteful .
docker images demo:wastefulWrite the size down. This is the number you will compare against.
Step 2: Find large layers with docker history
docker history is the fast first pass. It shows which instruction made each layer and how big the layer is.
docker history --format "table {{.ID}}\t{{.Size}}\t{{.CreatedBy}}" demo:wastefulLook for two patterns:
- A layer from
apt-get updatethat is much larger than you expect. Its package lists are still in the image. - A
COPYlayer the size ofdata.tar, followed by anrmlayer that shows 0B.
The second pattern is the key one. A later rm hides the file from the final filesystem, but the earlier layer still stores it. docker history shows the size, but it does not show which files cause it. That is the job of dive.
Step 3: Confirm the waste with dive
dive demo:wastefulSelect the COPY layer and check the file tree for /tmp/data.tar. Then select the rm layer. The file appears as removed, yet the earlier layer still holds it. Check the wasted-space list and the image efficiency score in the lower-left panel. Note both values.
Step 4: Fix the Dockerfile
Apply two changes. Run the cleanup in the same RUN that created the files. Use a BuildKit bind mount so data.tar never enters a layer.
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 /optIf the image contains build tools, a multi-stage build is the stronger fix. Compile in one stage and copy only the result into a clean final stage. Add a .dockerignore file so unneeded files in the build context never reach a COPY.
Step 5: Rebuild and verify
Rebuild under a new tag so both versions stay available for comparison.
docker build -t demo:lean .
docker images demo
docker history demo:lean
dive demo:leanCheck three things:
- The total size of
demo:leanis smaller thandemo:wasteful. - No layer in
docker historyis larger than the content it adds. - In
dive, the wasted space is near zero and the efficiency score has risen.
If a large layer remains, return to Step 2. The loop ends when the remaining layers contain content the image needs.
Step 6: Lock it in with CI
Once the image is clean, stop regressions with a .dive-ci file in the project root.
rules:
lowestEfficiency: 0.95
highestWastedBytes: 20MB
highestUserWastedPercent: 0.20Run dive without the interface.
CI=true dive demo:leanDive reads the rules and exits with a non-zero status if any rule fails, which fails the pipeline step. Set the thresholds just beyond your measured values. If a threshold is far looser than your image, it will not catch a regression. If it is stricter than the image can meet, the check fails on every build.
Common mistakes
- Comparing sizes across different base image tags. Pin the base so the only change is yours.
- Deleting files in a later
RUNand expecting the image to shrink. - Trusting the efficiency score alone. A small image can score low, and a large image can score high. Use the score together with the actual byte counts.
Exercise
After the fix, docker history shows the same total size as before, and dive still reports wasted space from data.tar. Which Dockerfile line should you check first, and why?
Show answer
Check the COPY data.tar /tmp/data.tar line. If the fix still copies the file into a layer, a later rm cannot reclaim the space. Replace the COPY with a RUN --mount=type=bind that extracts the archive in a single step, so data.tar is never written to a layer.