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.
- Windows: https://docs.docker.com/desktop/install/windows-install/
- Ubuntu linux distribution: https://docs.docker.com/desktop/install/ubuntu/
- MacOs: https://docs.docker.com/desktop/install/mac-install/
For the demo, Docker Desktop for Windows is installed.
- Open Docker Desktop.
To import images from the docker hub registry, select the Images option and sign in with the docker hub credentials.
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.
To get the detailed analysis of the image, select the dive-In option from Extensions, choose an image and 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.
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.
- To get a detailed
static analysis
of the docker image and get itsDockerfile
through reverse engineering
docker-slim xray --target nginx:latest
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>
- 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>
To check the history
docker history <dockerhub-username>/<squashed_image_name>:<tag>
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.
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 withdocker build
DOCKER_BUILDKIT=1 docker build -t <dockerhub-username>/<image_name>:<tag> .
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>
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.