DEV Community

How I Reduced Docker Image Size from 1.43 GB to 22.4 MB

Mohammad Faisal on May 05, 2023

To read more articles like this, visit my blog If you are working in web development, then you probably already know about the idea of containeriz...
Collapse
 
rahuls profile image
Rahul

Hello Faisal,

Really insightful blog post. I just got a chance to work with docker and some parts are easy and other parts are bit difficult for me.

I have few questions here:
1) After building the second image in the multi stage containerization, will the first image get discarded automatically?
2) Why is that the node server is not good for serving static content? Why is Nginx preferred? Is it because Nginx is good for production.

Thank you.

Collapse
 
mohammadfaisal profile image
Mohammad Faisal
  1. yes
  2. NodeJS is not optimized to serve static assets. nginx is specificly designed for acting as a high performance web server. That's why it's good
Collapse
 
timhub profile image
Tech Tim (@TechTim42) • Edited

good practice.

I have never thought about to wrap nginx in an app level container, but it does offer a good option to optimise web app docker size

Collapse
 
mohammadfaisal profile image
Mohammad Faisal

Right. It's probably better to use s3 or something to host a simple static site.

But if you go down the docker path, nginx is the best choice.

Collapse
 
viniciuslagogehrke profile image
Vinicius Lago Gehrke

Interesting and helpfull article. But there's a trick here that's more React then Docker: you move from a development build on step3 wich is built focused on Dev Experience (with Hot Reload and more), to a production build on step4 that serves the static files and minified JS that is way smaller.

The thing is they serve different proposals:
The first dockerfile can be used so every developer can run it on their machines with the same environment without installing anything apart from Docker

The second dockerfile is focused on serving the final content to production

Collapse
 
jon_snow789 profile image
Jon Snow

Great Article 🧡👍

Collapse
 
getcodescandy profile image
codescandy

Awesome thank you.

Collapse
 
quocbao64 profile image
quocbao64

Nice post, thank you for sharing

Collapse
 
cuongquang profile image
cuongquang

Nice post! I never compare docker container size this way before ❤️

Collapse
 
msnisha profile image
Nish

I was thinking that is what everyone was doing using nginx to serve ReactJS application usign nodejs is definitely an overkill and I never tried using it for statics websites. Nginx containers are so light you can run many containers in on raspberry pi you could host your websites from home when you are starting with new applications.

Collapse
 
mohammadfaisal profile image
Mohammad Faisal

Well.... now you know :P

Collapse
 
neelbrahmakshatriya profile image
Neel Brahmakshatriya

Interesting article!

I was wondering that can the size of the docker image with yarn start be reduced without making a build?

Collapse
 
madeindra profile image
Made Indra • Edited

Nice post! I hope you could try this idea for your next blog post.

Use Node v20 to build a single executable of your app and then use scratch for the base image in the second stage

Collapse
 
luckyq7 profile image
lucky

good