As you may already know, a Docker container image is a lightweight, standalone, executable package of software that has everything required to run an application. That's why container images are often used by developers for building and distributing applications. If you're curious to know what is in a Docker image, this brief guide might help you. Today, we are going to learn to analyze and explore the contents of Docker images layer by layer using a tool named "Dive".
By analyzing a Docker image, we can discover possible duplicate files across the layers and remove them to reduce the size of the docker image.
The Dive utility is not just a Docker Image analyzer, but also helps us to build one. Dive is free, open source tool written in Go programming language.
Install Dive in Linux
Get the latest version from the releases page and install it as shown below depending upon the distribution you use.
If you're on Debian or Ubuntu, run the following commands to download and install it.
$ wget https://github.com/wagoodman/dive/releases/download/v0.0.8/dive_0.0.8_linux_amd64.deb
$ sudo apt install ./dive_0.0.8_linux_amd64.deb
$ wget https://github.com/wagoodman/dive/releases/download/v0.0.8/dive_0.0.8_linux_amd64.rpm
$ sudo rpm -i dive_0.0.8_linux_amd64.rpm
It is available in AUR, so you can install using any AUR helper tools, for example Yay, on Arch Linux and its variants.
$ yay -S dive
Dive can also installed using Linuxbrew package manager.
$ brew tap wagoodman/dive
$ brew install dive
For other installation methods, refer the project's GitHub page given at the end of this guide.
Analyze And Explore The Contents Of Docker Images
To analyze a Docker image, simply run dive command with Docker "Image ID". You can get your Docker images' IDs using "sudo docker images" command.
$ sudo dive ea4c82dcd15a
Here, ea4c82dcd15a is Docker image id.
The Dive command will quickly analyze the given Docker image and display its contents in the Terminal.
As you can see in the above screenshot, the layers of given docker image and its details, wasted space are shown in the left pane. On the right pane, the contents of each layer in the given Docker image. You can switch between the left and right pane using Ctrl+SPACEBAR key and UP/DOWN arrow keys to navigate through the directory tree.
The list of keyboard shortcuts to use "Dive".
- Ctrl+Spacebar - Switch between left and right panes,
- Spacebar - Expand/Collapse directory tree,
- Ctrl+A - Show/hide added files,
- Ctrl+R - Show/hide removed files,
- Ctrl+M - Show/hide modified files,
- Ctrl+U - Show/hide unmodified files,
- Ctrl+ L - Show layer changes,
- Ctrl+A - Show layer changes,
- Ctrl+/ - Filter files,
- Ctrl+C - Exit.
In the above example, I have used "sudo" permission, because my Docker images are stored in /var/lib/docker/ directory. If you have them on your $HOME directory or anywhere not owned "root" user, you need not to use "sudo".
You can also build a Docker image and do an immediate analysis with one command:
$ dive build -t <some-tag>
For more details, look into the project's GitHub page given below.