Sub topic 6
Installing and running dive
This chapter assumes you can build and run images, read a simple Dockerfile, and use a Linux command line. It also assumes you have read how docker history lists layers. Here you install dive and open your first image in it.
What dive adds to docker history
docker history shows one line per layer: its size and the instruction that created it. It does not show which files a layer added, changed, or removed. dive reads the image layers and shows the file tree of each one. That lets you see why a layer is large and whether a later layer hid data from an earlier one.
dive needs the image on a Docker-compatible engine. It can read from the local Docker daemon, which is the default, or from other sources such as Podman or a saved image archive.
Installing dive
Pick the method that fits your system. Check the project's README for the current release before you install.
| Platform | Command |
|---|---|
| macOS (Homebrew) | `brew install dive` |
| Arch Linux | `pacman -S dive` |
| Debian or Ubuntu | Download the `.deb` from the GitHub releases page, then run `sudo apt install ./dive_*_linux_amd64.deb` |
| Any system with Docker | Run dive itself as a container, shown below |
The container option needs no install. It mounts the Docker socket so dive can read local images:
docker run --rm -it \
-v /var/run/docker.sock:/var/run/docker.sock \
wagoodman/dive:latest demo:wastefulConfirm a local install worked:
dive --versionRunning dive
You need an image to inspect. Build the wasteful example from the earlier chapters. Its Dockerfile leaves the apt lists in the image and keeps data.tar in a layer even though a later step deletes it.
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.tarThere are two ways to open it in dive:
dive demo:wasteful
dive build -t demo:wasteful .The first command opens an image that already exists locally. The second runs the build for you, using the current directory as the build context, and opens the result when it finishes. dive build accepts the same flags as docker build, so -t, -f, and --build-arg work as you expect. Use it when you are iterating on a Dockerfile, because one command rebuilds and reopens the analysis.
If the image name is not found locally, dive tries to pull it. A typo in the tag can therefore look like a slow network call instead of an immediate error.
What you see on first launch
dive opens a full-screen terminal interface with two panes. The left pane lists the image layers, one row per layer, along with a summary of size and efficiency. The right pane shows a file tree for the selected layer. The next chapter covers navigation in detail. For now, check three things.
- The layer list should have the same number of layers as
docker historyshows for the same image, apart from empty entries. - The
COPY data.tarlayer should be large. - The
rmlayer should appear tiny, even though it removes the file.
That third point is the core lesson. A later layer cannot shrink an earlier one. The rm step only records that the file is gone, and the bytes stay in the COPY layer.
Common problems
- Cannot connect to the Docker daemon. dive cannot reach the engine. Check that Docker is running and that your user can run
docker ps. In the container form, check the socket mount. - Garbled layout. The terminal is too small or does not support the interface. Enlarge the window and retry.
- Wrong source. If you use Podman, pass
--source podman(or set it in your config) so dive does not look for the Docker daemon. - Image not found. Run
docker imagesand check the exact name and tag.
Exercise
You changed one line in a Dockerfile and want to rebuild and inspect the result in a single command. Which dive command do you run, and how does it differ from dive demo:wasteful?
Show answer
Run dive build -t demo:wasteful .. It builds the image from the current directory and then opens it in dive. dive demo:wasteful only opens an image that already exists locally and does not build anything.
Exercise
In dive, the rm /tmp/data.tar layer shows almost no size, but the image is still large. Why?
Show answer
Layers are additive. The rm instruction adds a layer that marks the file as deleted, but the COPY layer still stores the file's bytes, so the image size does not shrink.