Signup/Sign In
LAST UPDATED: AUGUST 1, 2023

Optimize Docker Images with Dive tool!

    Docker images are the building blocks of containers, as they contain the instructions required to create and run them. However, some images may be larger, more complex, or less efficient than others, depending on how they are built and what they contain.

    Which can affect the performance, security, and maintainability of your applications. Therefore, it is crucial to understand and optimize Docker images.

    For this, you can use a tool called dive. In this article, we will show you how to install and use dive, what features it offers, and how it can help you with your Docker image analysis.

    What is Dive?

    Dive is an open-source tool that allows you to explore a Docker/OCI image, layer by layer, its contents and discover ways to shrink its size and improve its efficiency.

    Dive into layers of docker images

    Dive works in the following steps:

    1. Fetch the image from a local or remote source, such as Docker hub or local Docker daemon using Docker API.
    2. Analyzing image layers
      1. Layers are the incremental changes that are applied on top of each other to form the final image.
      2. Each layer corresponds to a command in the Dockerfile that was used to build the image.
    3. Dive then displays interactive CLI with three sections:
      1. A list of layers on the left side, with their sizes and commands.
      2. A file tree of the selected layer on the right side, with the files and directories that were added, modified, or deleted in that layer.
      3. A status bar at the bottom shows the total image size, the potential wasted space, and the efficiency score.

    It allows you to interact using keyboard shortcuts or mouse clicks.

    The potential wasted space is the amount of space that could be saved by removing unnecessary or duplicated files from the image. The efficiency score is a metric that indicates how well the image is optimized, based on the ratio of wasted space to total image size.

    How to Install Dive?

    There are different ways to install dive, depending on your operating system. Let's take a look at some of them.

    The latest release of Dive tool could be found in GitHub release page.

    Linux

    1. Ubuntu/Debian

    Download latest deb release and install with apt command:

    export 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 apt install ./dive_${DIVE_VERSION}_linux_amd64.deb
    1. RHEL/CentOS

    Download latest rpm release and install with rpm command:

    export 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.rpm
    rpm -i dive_${DIVE_VERSION}_linux_amd64.rpm
    1. Arch Linux

    You can install dive from the Arch User Repository (AUR) using pacman or yay directly as follows:

    pacman -S dive
    1. Nix/NixOS

      On NixOS:

      nix-env -iA nixos.dive

      Other than Nix OS

      nix-env -iA nixpkgs.dive

    Mac

    If you use Homebrew, you can install dive using brew package manager:

    brew install dive

    If you use Mac Ports, you can install dive using port:

    sudo port install dive

    Or, you can download the latest Darwin build from the GitHub releases page and run it directly.

    Windows

    You can download the latest Windows build from the GitHub releases page and run it directly.

    Alternatively, you can use Docker to run dive as a container:

    docker run --rm -it \
      -v /var/run/docker.sock:/var/run/docker.sock \
      wagoodman/dive:latest

    The above command will mount your local Docker socket into the container, allowing dive to access your local images.

    Verify dive installation:

    docker dive tool helpl commands

    How to Use Dive?

    To use dive, you need to have a Docker image that you want to analyze. You can either use an existing image from a Docker registry or a local Docker daemon, or you can build a new image from a Dockerfile.

    To analyze an existing image, simply run dive with the image tag, ID, or digest:

    dive <your-image-tag>

    To build and analyze a new image, you can build an image:

    dive build -t <some-tag> <context>

    For example, to build and analyze an image from the current directory using the Dockerfile in it, you can run:

    dive build -t my-image .

    This command will build the image using docker build and launch the interface.

    For example, to analyze the official nginx image, you can run:

    dive nginx:latest

    It will fetch the image from the Docker Hub and launch the below interface

    Docker dive tool tutorial

    Once you are in the dive GUI, you can use the following keyboard shortcuts or mouse clicks to interact with it:

    Keyboard Shortcut Description
    Up/Down or k/j Move up/down one layer
    PageUp/PageDown Move up/down one page of layers
    Home/End or g/G Move to the first/last layer
    Right/Left or l/h Expand/collapse directory
    Enter View file contents
    / Filter files by name
    s Sort files by name/size
    Tab Toggle file tree view and layer comparison view
    Space Add/remove layer from comparison view
    Ctrl-n Show next difference in comparison view
    Ctrl-p Show previous difference in comparison view
    ? Show help menu
    Ctrl-c Quit dive

    How to Optimize Your Docker Image with Dive?

    To optimize your Docker image (remove unnecessary stuff) with dive, you can follow these steps:

    1. Check its total size, wasted space, and efficiency score of the image.
    2. Identify layers that contribute most to wasted space and see what files they contain.
    3. Use the filter and sort options to narrow down your search.
    4. Review the commands that created those layers and see if you can modify them to avoid adding unnecessary or duplicated files.
      For example, use multi-stage builds, combine multiple commands into one layer, remove temporary files or caches, exclude unwanted files with .dockerignore, use smaller base images, etc.
    5. Rebuild your image with the modified commands and re-analyze it with dive.

    See if the wasted space and efficiency score have improved.

    Repeat steps 2 to 5 until the image is optimized enough to your satisfaction.

    Conclusion

    This is how you can use dive to analyze existing images or build new ones, and interact with them to improve the performance, security, and maintainability of your applications using containers.

    We hope you enjoyed this article and learned something new about dive and Docker images. Thank you for reading!

    Pradeep has expertise in Linux, Go, Nginx, Apache, CyberSecurity, AppSec and various other technical areas. He has contributed to numerous publications and websites, providing his readers with insightful and informative content.
    IF YOU LIKE IT, THEN SHARE IT
    Advertisement

    RELATED POSTS