Sub topic 7
Exploring layers and file trees in dive
This chapter assumes you can build and run Docker images, read a simple Dockerfile, and use a Linux terminal. It also assumes you have installed dive and built the demo:wasteful image from the earlier chapters. Here you learn how to read dive's interface so you can see what each layer adds, changes, or removes.
Opening an image
Dive has two entry points. The first opens an image that already exists locally. The second builds the image from the current directory and opens it in one step.
dive demo:wasteful
dive build -t demo:wasteful .Both open a full-screen terminal interface. It shows the same layers that docker history listed, but it also reads the files inside each layer.
The layout of the interface
The screen is split into panes.
| Pane | Position | What it shows |
|---|---|---|
| Layers | Left, top | One row per layer, with its size and the instruction that created it |
| Layer Details | Left, middle | The ID, total size, and command of the selected layer |
| Image Details | Left, bottom | Total image size, wasted bytes, and the efficiency score |
| Current Layer Contents | Right | The file tree for the selected layer, or for all layers up to it |
flowchart LRThe diagram below shows how the panes relate to each other.
Moving around
Use the keyboard. These keys work in the default configuration:
UpandDownmove the selection within the active pane.Tabswitches focus between the Layers pane and the file tree pane.Ctrl+Lswitches the file tree between layer-only view and aggregated view.Spacecollapses or expands a directory in the file tree.Ctrl+A,Ctrl+R,Ctrl+M, andCtrl+Utoggle the display of added, removed, modified, and unmodified files.Ctrl+Corqexits.
Key bindings can change between dive versions. The legend at the bottom of the screen shows the bindings for your version.
Layer view and aggregated view
The file tree has two modes, and the difference matters.
- Layer view shows only the changes the selected layer made. Use it to answer "what did this instruction add or delete?"
- Aggregated view shows the filesystem as a container would see it after that layer. Use it to answer "what is in the image at this point?"
Select the COPY data.tar /tmp/data.tar layer in demo:wasteful. In layer view, /tmp/data.tar appears as an added file. Move down to the RUN rm /tmp/data.tar layer. The tree now shows data.tar as removed. The file is gone from the final filesystem, but the earlier layer still stores it, so the image size does not shrink.
Reading the colours
Dive marks each path in the tree.
| Marker | Meaning |
|---|---|
| Green, `+` | The layer added this file |
| Yellow, `~` | The layer modified this file |
| Red, `-` | The layer removed this file |
| No colour | The layer did not touch this file |
Directories are coloured by what happened to their contents, so you can find the interesting changes without opening every folder.
Finding wasteful layers
Use this routine with demo:wasteful.
- Select the layer for
RUN apt-get update. In layer view, expand/var/lib/apt/lists. These package index files are added here and never removed, so they stay in the final image. - Select
RUN apt-get install -y curland look at its size. Package files and cached data add to it. - Select
COPY data.tar /tmp/data.tar. Note the size of the added tar file. - Select
RUN rm /tmp/data.tar. Dive shows the removal, and the layer itself is tiny. - Check Image Details. It reports wasted bytes because the removed file still takes space in an earlier layer.
Common mistakes
- Reading the aggregated view as layer cost. Aggregated view shows the final filesystem, so deleted files are missing. It hides the waste. Switch to layer view to see what each layer stores.
- Trusting a small final layer. A layer that deletes a large file is tiny, but the image is still large.
- Forgetting to use an unmodified view. If the tree looks empty, one of the filters, such as the unmodified-files toggle, may be hiding entries.
- Confusing
<missing>IDs with problems. Dive reads layers from the image data directly, so it does not depend on those IDs the waydocker historyoutput does.
Exercise
In dive, you select the layer created by RUN rm /tmp/data.tar and see data.tar marked as removed. The image is still large. Which pane and view help you find where the space is spent, and why?
Show answer
Select the earlier COPY data.tar /tmp/data.tar layer in the Layers pane and use layer view in the file tree. The removal layer only records a deletion, so it is small. The tar file is still stored in the COPY layer, where it shows as an added file and accounts for the size. Image Details also lists this as wasted bytes.