DEV Community

Cover image for Reducing Docker Image size

Reducing Docker Image size

Pratik Singh on January 04, 2023

This article will cover the basics to reduce the size of your Docker images. Prerequisites ✅ Understanding of Docker: Here An existin...
Collapse
 
ervin_szilagyi profile image
Ervin Szilagyi • Edited

If you really need to shrink the size of a Docker image instead of Alpine you may want to use a distroless image. Moreover, if you have only one executable to dockerize and you don't need to emulate an OS environment, you can use the scratch image and run the executable as PID 1.

Collapse
 
kitarp29 profile image
Pratik Singh

Will try that on next time!
Thank you so much for sharing :)

Collapse
 
lankavitharana profile image
Rajith

yeah, I have tried some of these approaches, the best result I got was with scratch, with that, service in go in docker is reduced to ~8mb (just the executable only), I did the same with Rust as well, and got that reduced even more AFAIR, if you use scratch as the base image(scratch means empty, IE you only pack the executable to the docker image, that essentially means, it's mostly upto the compiler to reduce the executable size, with go and Rust, it is easy to do), you will have to cross compile and both go and Rust has compiler flags to do that

Thread Thread
 
kitarp29 profile image
Pratik Singh

That's great! Thanks Rajith for sharing this insight.
Hope this article helped :)

Collapse
 
cloutierjo profile image
cloutierjo

Also, one of the most neglected part is to condensate run statement, if you have 10 command to execute, running them all in a single RUN will create only one layer, bonus point if some of those command create temporary files, you can erase them at the end of that RUN, they won't be present at all in the final image. In particular Linux update can get a major gain since you can delete the package cache, index and the repo index that are totally useless once the update is completed.

Collapse
 
kitarp29 profile image
Pratik Singh

Will give it a try!

Collapse
 
nareshchandanbatve profile image
Naresh Chandanbatve

Hey @kitarp29
I have heard that using lighter base images like alpine are not official, also they are not well tested, so is it really safe to use these images in production?

Or should I use other methods to reduce image size?

Collapse
 
kitarp29 profile image
Pratik Singh • Edited

Don't use unofficial images in Production ever!
You can introduce a job in your CI pipeline to scan for vulnerabilities in your Docker images.
Some of the tools that help you with this are:

  • Docker Scout
  • SonarQube
  • JFrog
Collapse
 
bcostaaa01 profile image
Bruno

You can also achieve that by using a container registry, which can automatically compress images, hence reducing their size.

Reducing the size of a Docker image helps massively reducing the download time and much less storage, which is the main idea of your article.

Great article in general, fellow writer @kitarp29!👏

Collapse
 
kitarp29 profile image
Pratik Singh

Thanks Bruno! I will keep that in mind.
Your kind words means a lot ot me :)

Collapse
 
pavanbelagatti profile image
Pavan Belagatti

Amazing post Pratik.

Collapse
 
kitarp29 profile image
Pratik Singh

Thank you Pavan

Collapse
 
shirome9 profile image
SHiRoMe9

Thank you for the article! Just curious which is slim reduction of Docker to make this big difference size?

Collapse
 
veerreshr profile image
Veeresh

Great post, it would be helpful if you included some stats like how much time it took to spin container with older and newer image.

Collapse
 
kitarp29 profile image
Pratik Singh

Veeresh I don't have the exact metric for it. But safe to assume it takes atleast half of the time.

Collapse
 
bruc3mackenzi3 profile image
Bruce MacKenzie

"Further reduced the size of the binary further and finally got a size of around 16 MB."

What steps did you take for this further reduction in image size?

Collapse
 
kitarp29 profile image
Pratik Singh

Well this article tells the entire story of it.
You can read more about it in this Twitter thread I was a part of:
twitter.com/sidpalas/status/163783...

Collapse
 
mfurmaniuk profile image
Michael

We're about to do this with the images we build at work, so this is very timely.

Collapse
 
kitarp29 profile image
Pratik Singh

I am glad I could help :)
Thanks for reading my article 😃

Collapse
 
shoaib19 profile image
محمّد شعیب

Great article🔥

Collapse
 
kitarp29 profile image
Pratik Singh

Thanks 😌

Collapse
 
amykhar profile image
amykhar

Very practical advice. Thank you.

Collapse
 
kitarp29 profile image
Pratik Singh

Thanks :)
Hope it helps!

Collapse
 
yrs147 profile image
Yash Raj Singh

Really insightful Blog Pratik

Collapse
 
kitarp29 profile image
Pratik Singh

Thanks! Hope it helped :)

Collapse
 
nikki_eke profile image
Nikki Eke

Quite insightful Pratik, thanks for sharing!

Collapse
 
kitarp29 profile image
Pratik Singh

Thanks Nikki!
Hope it helps 😊