I'm all new to Reddit, and since I had some fun learning Docker lately I thought it'd be fun to join the community here :).
I wrote a 2 part blog post about my journey reducing the size of the Docker image of a [well known](https://carbon.now.sh/) nodejs application as much as I could. I managed to go down to 100MB, from a starting point of 2.43GB when doing it 'quick and dirty'.
[Maybe some of you like it!](https://lengrand.fr/reducing-dockers-image-size-while-creating-an-offline-version-of-carbon-now-sh/)
And if you have more advice, please enlighten me! As mentioned, I am learning in the process.
12 thoughts on “Reducing a node application’s Docker image size from 2.4GB to less than 100MB. Learning Docker step by step”
Just by reading , I could sense the joy of achievement.
Good, fun writeup. Well done
If you are serving a static site, you could use as a second stage image a nginx (could be nginx-alpine to reduce size) image, and serve with nginx the statics. There’s no need to have running a “heavy” node server.
Even better: Check out Google’s distroless project: https://github.com/GoogleContainerTools/distroless/
Beautiful. Seem like Go and Docker are better combination
Nice article, thanks for sharing. I didn’t know about dive, I’ll try it out next time I have to build something.
I believe you can simplify the multiple COPY commands using .dockerignore to filter out the files you don’t want in the image.
Did something similar recently. Node alpine and I copy over an entrypoint shell script and a single bundled file of my node app, was around 100 or so MB total. I don’t copy over all the node nodules.
Awesome blog post!
I created a docker-slim example for the app too. It has a very basic Dockerfile version using the standard node image without multi-stage and without other optimizations resulting in an image that’s 2GB+. The optimized image is 93MB: https://github.com/docker-slim/examples/tree/master/3rdparty/carbon-now-sh
By the way, the xray command in docker-slim will give you something similar to what you get with Dive (no interactive shell though for now though).
I’m a bit curious, what is the difference with using `mhart/alpine-node` vs using the ~~actual~~ official `node:alpine` ? I’ve been looking through the 2 dockerfiles but can’t really see the difference 🙁
(Assuming we’re using the same Node LTS version 12.17.0)
A good 3-part article on reducing container sizes here : https://www.ardanlabs.com/blog/2020/02/docker-images-part1-reducing-image-size.html
Thanks for the info! Helped me reduce the size of my [watchtower](https://hub.docker.com/r/jakowenko/watchtower) image by easily switching to alpine. This was my first public image and I’ve relatively new to Docker, but it’s been a very fun learning experience!
Love the article. For the life of me i can’t understand something tho: how does splitting it into builder/runner containers save space? It looks like you are installing and building everything into /app in the builder and then copying everything in /app into the second container. Since the base alpine image is the same, i can’t see what space is being saved from the builder container. (Sorry if i’m missing something obvious!)