Docker lets you run an application within container environments. It allowed you to build images that let your application inherit characteristics such as portability, easy deployment, platform compatibility, etc. However, many encounter challenges with large Docker container sizes that take up significant storage space on your computer. Taking advantage of tools such as DockerSlim allows you to minimize Docker image size up to 30 times. This guide will teach how to minimize Docker image size using DockerSlim.
To continue in this article, it is helpful to have the following:
- Docker installed on your computer.
- Download DockerSlim from here.
- To check if DockerSlim has successfully been installed, check the version:
What is DockerSlim
DockerSlim allows you to build slimmer Docker images. It analyzes your Docker image’s contents and removes duplicate files, unused dependencies, and system libraries that the application doesn’t require to run. This significantly reduces Docker image size, making deploying and managing your application in different containerized environments easier.
DockerSlim uses static and dynamic analysis to reduce the Docker image layers. These approaches slim or remove layers with unnecessary binaries, libraries, files, and directories. As a result, they can optimize the container development process by making them smaller and more efficient.
DockerSlim also allows you to add custom configuration files and build scripts to fine-tune the optimization process to meet your specific application requirements. DockerSlim archives this using three commands, xray, lint, and profile. These commands provide analysis for your Dockerfile and Docker images to allow you to understand your container in detail.
Let’s dive and use these commands to learn how DockerSlim works to reduce and minimize Docker image sizes using DockerSlim
DockerSlim Lint Command
The lint command analyzes the container instructions in Dockerfile. It analyzes a Dockerfile and identifies potential issues and inefficiencies that could impact the image size. The lint command takes Dockerfile as input and performs a static analysis of its contents. It checks for common issues such as:
- Any files and directories not needed by the image.
- Redundant and unused dependencies.
- Misconfigured environment variables with security vulnerabilities.
- Dockerfile formats that can cause build failures.
To test its functionality, ensure you have a Dockerfile for your project. Below is an example Dockerfile with instructions for running a Node.js application as follows:
FROM node:16 WORKDIR /usr/src/app COPY package.json . RUN npm install RUN chown -R node /usr/src/app/node_modules COPY . . EXPOSE 4000 CMD ["node","app.js"]
On this file directory location, run a lint command to analyze the above instructions:
docker-slim lint --target Dockerfile
In this example, DockerSlim gives you a
warn to add a
.dockerignore file to your application.
.dockerignore allows you to specify files that Docker should avoid copying and can result in increased image size while still not needed to create the overall image build.
DockerSlim Xray Command
This command performs static analysis of the target container image. From this command, you can get to know what is inside the target container and what makes it large:
To test the command, create a simple Nginx image by pulling the Nginx Docker image as follows:
docker pull nginx:latest
Apply the xray command to the created image as follows:
docker-slim xray nginx
DockerSlim profile Command
The command analysis of the container image without generating an optimized image.
To test the command:
docker-slim profile --sensor-ipc-mode proxy --sensor-ipc-endpoint 172.17.0.1 --http-probe=false nginx;
Minimize Docker image size using DockerSlim Build Command
Now that you understand what you need to make your image slimmer, DockerSlim uses a build command to optimize your overall image size. The build command acts as a DockerSlim compression strategy. It gives you the ability to compress your Docker images. To use it, let’s check the created image’s original size:
docker images nginx:latest
From the above, the image size is 135MB.
To optimize the image, run the below build command:
docker-slim build --sensor-ipc-mode proxy --sensor-ipc-endpoint 172.17.0.1 --http-probe=false nginx;
Once the process is done, check the present Docker images, you should see an
Your image should be reduced to around 12.4MB.
This shows that DockerSlim can reduce and compress your image by approximately a 90.37% reduction from the original size.
This guide taught you how to analyze your images using the DockerSlim command. With that, you are able to understand your Docker image anatomy and know why it’s needed to optimize them and reduce the overall Docker image size. DockerSlim is a valuable tool for DevOps teams to optimize containerized workflows, thus helping you reduce infrastructure costs. I hope this helped you learn how to minimize Docker image sizes using DockerSlim.