Docker Image Analysis Tools: To Maximize the Efficiency and Minimize the Size

A Detailed Exploration of Open Source Tools for Precise Image Analysis

There are different kinds of analysis tools available to find out the way to minimize the size of docker image. These tools are used to analyze and explore the image contents layer by layer. It discovers duplicate files, shows image efficiency score, information about wasted space, inefficient bytes, total image size etc. By working with these tools one may get an idea how to build an efficient image without compromising its quality.

Analysis Tools

Following are some open source tools that will help in optimization.

  • Docker Dive
  • Docker Slim
  • Docker Squash
  • BuildKit

Docker-Dive

Docker Dive is an image analysis tool that helps one to find layers and gives a separate size of each layer and a list of files with size. Using dive-in, one can easily discover ways to optimize the image. It is written in the Go language. 

It is a tool for image analysis which one can use with docker desktop.

To install docker desktop on your OS. Click on the Docker Desktop for the respective OS and download the file from the site below.

For the demo, Docker Desktop for Windows is installed.

  • Open Docker Desktop.
Figure 1: Docker Desktop Page
Figure 1: Docker Desktop Page

To import images from the docker hub registry, select the Images option and sign in with the docker hub credentials.

Figure 2: Import images from DockerHub
Figure 2: Import images from DockerHub

To use the dive-in tool, select Add Extensions from docker desktop menu. It will redirect to Marketplace, search for a dive in and install it.

Figure 3: Install Dive In
Figure 3: Install Dive In

To get the detailed analysis of the image, select the dive-In option from Extensions, choose an image and analyze.

Figure 4: Choose an image to analyse
Figure 4: Choose an image to analyze

Retrieve the inefficient bytes, efficiency score, files with size and layers with size in detail. This analysis is helpful to minimize image size by removing unnecessary files and compressing the number of layers.

Figure 5: Image information
Figure 5: Image information
Figure 6: Files stored in image
Figure 6: Files stored in image
Figure 7: Image layers with size
Figure 7: Image layers with size

Dive is a helpful analysis tool but it is not enough. To be able to improve images, one needs to know how docker image building works. Please refer more about image build here.

Docker-Slim

Docker Slim is a SlimToolkit to build, inspect, optimize, and debug container images created by Slim.ai. It helps to minimize the docker images for security and, image size get smaller. 

  • To install docker on the ubuntu Linux distribution
apt update && apt install docker.io -y
  • To install docker-slim on ubuntu Linux distribution
wget https://downloads.dockerslim.com/releases/1.40.1/dist_linux.tar.gz
  • To extract the binary file from the .tar.gz file
tar -xvf dist_linux.tar.gz 
  • To move the extracted binary files to /usr/local/bin.
sudo mv dist_linux/* /usr/local/bin

Before start with docker-slim pull the image from dockerhub.

  • To pull an image from dockerhub
docker image pull nginx:latest
  • To start with docker slim
docker-slim help
  • To build an image with docker-slim
docker-slim build --target nginx:latest
  • To list the images and check the image size
docker image ls

Image size is significantly reduce with docker-slim build.

Figure 8: Verify Image size with docker-slim
Figure 8: Verify Image size with docker-slim
  • To get a detailed static analysis of the docker image and get its Dockerfile through reverse engineering
docker-slim xray --target nginx:latest
Figure 9: Partial output of xray option
Figure 9: Partial output of xray option

Every docker-slim commands generate a slim.report.json file. The xray option of  docker-slim add all the details of container image and provides a resource for identifying what is inside it in this file.

Explore more options like debug, lint, version, profile etc. here

Docker-Squash

Docker Squash is a utility to squash multiple layers into a single layer to create an image with small size with good quality.

Note: Install docker from previous section if it is not installed.

  • To install docker-squash with pip
apt install pip && pip install docker-squash
docker-squash --help
  • To write a Dockerfile
vim Dockerfile 
FROM python:3.9
RUN apt-get update
RUN apt-get install nginx -y
RUN apt-get install php -y
  • To build an image
docker image build -t <dockerhub-username>/<image_name>:<tag> .
  • To get the detailed history of image layers
docker history <dockerhub-username>/<image_name>:<tag>
Figure 10: History of image layers
Figure 10: History of image layers
  • To squash all the layers down to a particular layer
docker-squash -f <layer_id> -t <dockerhub-username>/<squashed_image_name>:<tag> <dockerhub-username>/<image_name>:<tag>
Figure 11: Squash the image layer
Figure 11: Squash the image layer

To check the history

docker history <dockerhub-username>/<squashed_image_name>:<tag>
Figure 12: Squashed image history
Figure 12: Squashed image history

Another option is to specify how many layers one needs to squash.

  • To squash last 3 layers from the <dockerhub-username/image_name:tag> image
docker-squash -f 3 -t <dockerhub-username>/<squashed_image_name>:<tag> <dockerhub-username>/<image_name>:<tag>

This command gives the same result as above. 

  • To list the images and check the image size
docker images

Layers are squashed and image size is decreased. 

Figure 13: Compare image size
Figure 13: Compare image size

BuildKit

BuildKit comes with docker version 23.0 as default builder on Docker Desktop and Docker Engine.It helps in improving the build’s performance. It searches the unused build steps and skips those steps. It only builds the stages that the target stage depends on. Refer more about BuildKit here.

  • Create a myapp directory
mkdir myapp && cd myapp
  • Create Dockerfile in myapp
vim Dockerfile
FROM fedora
CMD ["/bin/echo","Hello Pratiksha"]
  • To build an image
docker image build -t <dockerhub-username>/<image_name>:<tag> .
  • To build an image with buildkit, set the DOCKER_BUILDKIT environment variable with docker build
DOCKER_BUILDKIT=1 docker build -t <dockerhub-username>/<image_name>:<tag> .
Figure 14: image built layer by layer
Figure 14: image built layer by layer

To use the Docker BuildKit by default, edit docker daemon configuration /etc/docker/daemon.json  and add the following. Then restart the docker.

{
  "features": {
    "buildkit": true
  }
}

While creating an image one gets a detailed analysis of the image to be built and skip the intermediate stages if it is not required in the last stage.

  • To run a container from both images
docker run <dockerhub-username>/<image_name>:<tag>
Figure 15: Running Container from both images
Figure 15: Running Container from both images

Summary

These tools are open source and freely available so, widely used in docker image optimization. One can think of how to minimize an image if there is a detailed analysis and a knowledge about image build. I would love to hear comments from more people for betterment.

Join Our Newsletter

Share this article:

Table of Contents