Containerised applications frequently suffer from image bloat, where unnecessary build dependencies, temporary package manager caches, and intermediate layer artifacts inflate container sizes into gigabytes. Diagnosing exactly which layer contributed excessive weight or introduced redundant files can be difficult using standard docker history outputs alone. The dive tool provides a terminal-based user interface specifically engineered for exploring Docker and OCI images, inspecting layer contents, and discovering wasted space.
Why Use dive for Container Optimisation?
While Docker CLI commands detail the size of individual layers, they do not show the underlying filesystem modifications line-by-line. The dive tool solves this limitation through several critical features:
- Layer-by-layer filesystem tree: View the exact file additions, modifications, and deletions made at each step of your Dockerfile.
- Wasted space estimation: Automatically detects duplicated files, overwritten binaries, and poorly placed cache removals across image layers.
- Image efficiency score: Generates an overall percentage score rating how effectively the container avoids redundant payloads.
- CI/CD build pipeline integration: Can run headless in automation scripts to fail builds if wasted space exceeds a predetermined threshold.
Installing dive on Linux Distributions
The dive utility is distributed as standalone binaries, package archives, and a self-contained container image.
Installing on Ubuntu and Debian
Download and install the official Debian package from the GitHub releases page using curl and dpkg:
DIVE_VERSION=$(curl -sL "https://api.github.com/repos/wagoodman/dive/releases/latest" | grep '"tag_name":' | sed -E 's/.*"v([^"]+)".*/\1/')
curl -OL "https://github.com/wagoodman/dive/releases/download/v${DIVE_VERSION}/dive_${DIVE_VERSION}_linux_amd64.deb"
sudo dpkg -i "dive_${DIVE_VERSION}_linux_amd64.deb"
rm "dive_${DIVE_VERSION}_linux_amd64.deb"
Installing on Arch Linux
Arch Linux users can install dive directly from the community repository:
sudo pacman -S dive
Running dive via Docker (No Local Installation)
If you prefer not to install local packages, run dive directly as an ephemeral Docker container mounted to your local Docker socket:
docker run --rm -it \
-v /var/run/docker.sock:/var/run/docker.sock \
wagoodman/dive:latest <image-tag>
Inspecting an Image with dive
To inspect any local container image, execute the command followed by the target image repository and tag:
dive my-app:latest
Alternatively, if you are building an image from source, you can instruct dive to build and inspect it in a single command:
dive build -t my-app:latest .
Navigating the dive Terminal Interface
The dive interface splits your terminal into two primary columns:
- Left Column (Layers): Displays the Dockerfile commands in chronological execution order alongside their size and layer hash.
- Right Column (Current Layer Contents): Renders the complete filesystem hierarchy as it existed following the selected layer’s execution.
Key Navigation Shortcuts
- Tab: Switch focus between the Layers pane and the Filesystem tree pane.
- Up / Down Arrows: Scroll through individual layers or browse directory trees.
- Space: Expand or collapse highlighted directory nodes in the filesystem view.
- Ctrl + F: Filter files by keyword or file extension.
- Ctrl + A: Toggle visibility of unmodified files, highlighting only added, modified, or deleted items.
Interpreting Colour Codes and Identifying Waste
In the filesystem tree, dive uses intuitive colour coding to denote file operations:
- Green (+): Files added during this specific layer.
- Yellow (~): Existing files modified by this layer.
- Red (-): Files deleted in this layer.
Crucially, if a file is copied in Layer 2 and subsequently deleted in Layer 4, the file still occupies space inside the final image. The Image Details section at the bottom left calculates this as Wasted Space. To fix this pattern, combine the creation and deletion steps into a single RUN instruction or leverage modern multi-stage builds.
Automating Image Efficiency in CI/CD Pipelines
You can enforce container hygiene within continuous integration pipelines using the --ci flag:
CI=true dive my-app:latest --ci-config .dive-ci.yaml
Within your .dive-ci.yaml configuration file, establish strict rules for pass/fail criteria:
rules:
lowestEfficiency: 0.95
highestWastedBytes: 20MB
highestUserWastedPercent: 0.10
If a developer introduces an unoptimised layer that violates your efficiency thresholds, the build automatically fails before the container enters production registries.