This article will cover the basics to reduce the size of your Docker images.
Prerequisites ✅
- Understanding of Docker: Here
- An existing image or an application that needs a Docker image.
🤔 Understanding the Problem
In the world of microservices, most of the production is standing on containers. Images are like the CDs/Flash drives used to play a certain song (application) in your container.
Docker Images are made to bring unity among the developers despite the platform and environment. But obtaining this at cost of memory and speed is not acceptable.
- Bulkier images take time to get pulled.
- Increased time to spin up a container using it.
- It increases the load on the image registry used.
- With upgrades, it increases and becomes harder to check for vulnerabilities
I ran into the same issue in my pet project Kube-ez. The issue was around reducing the Docker image : Here. This was the first issue I resolved in 2023. Reduced the size by 97%. Turned the image from ~500 MB to 16 MB.
🤷🏻♂️ Why solve this Problem?
So what if the Docker image is 6 GB (..ahem Oracle 😑), it still runs right? Don't touch it.
Well, I disagree! Speaking from my experience as a Site Reliability Engineer at Juspay (Indian Fintech Startup). When there is a service that is running in production. Keeping it up and running is always a majority challenge. But let's agree that no company has an SLO of 100%. Each second of downtime counts!
Someday or the other the service is bound to crash or run into any error. In the best-case scenario, you will be updating it someday. That day you have to spin up a new container or an updated container, that's the day it helps!
A lighter Docker Image ensures that containers will spin up faster and thus saves precious seconds for your company!
Also, it's easier to debug, secure, and store :)
🐋 How to reduce the Docker Image Size?
Well, there are three approaches I found, and will be sharing all of them.
1. Docker Slim ✨
Docker Slim is the best and easiest tool to solve this problem.
Thanks to my friend Mritunjay, for introducing me to this tool.
- Easily reduce the size of your Docker Images.
- Connects to DockerHub and gives you an easier platform to handle images on the registry.
- It also checks and fixes vulnerabilities 🤯
- Installation and usage are super easy too!
If you wish to slim the size of a local Docker Image, try this article by RedHat Developer Group: Here.
Although, I would love to see apk and other package installation methods in the future.
Th results I got for the same Docker Image using the tool
2. Manual Approach 💪
Now, this requires a good understanding of your project. There are further two steps in this as well. You could stop after the first step if it suffices your needs. Let's get digging!
2.1 Using Lighter Base Images
This is the easiest and the first way to go for. Use a lighter image as a base layer for your container. You could go for an Alpine image of the OS or the language you are using.
Alpine Image: Images with basic barebone OS. It misses essential software and drivers like graphics, wifi, and more.
2.2 Multistage Build
The idea is to use two different stages in the image. One is to build the binary and the next is to simply use it!
Easier said than done 😅
In my case, I applied a multistage build pattern. Made a binary on the build stage and used it to run the container.
Now the image size was: ~35 MB. Further reduced the size of the binary further and finally got a size of around 16 MB.
There are multiple suggestions to include even after this. But I haven't implemented them yet.
- Include a .dockerignore file.
- Reduce unnecessary dependencies.
- Caching layers to reuse
3. Docker init 🐋
[Updated on 10/20/23]
Docker has just released a feature called docker init which automates this entire process. It not only works on the size of the image. But also follows the best practices of creating a Docker Image. I am still trying it with different projects. And the preliminary tests seems to be pretty impressive.
Read more on Docker Init : Here
I will keep on adding more ways as I find them.
If you liked this content you can follow me or on Twitter at kitarp29 for more!
Thanks for reading my article :)