top of page
GeekGuy

How to reduce Docker image size

This article will cover the basics to reduce the size of your Docker images.

Prerequisites

  • Understanding of Docker

  • 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

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 o 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 two approaches I found, and will be sharing both 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.

The 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 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


8 views0 comments

Related Posts

See All

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
Stationary photo

Be the first to know

Subscribe to our newsletter to receive news and updates.

Thanks for submitting!

Follow us
bottom of page