Skip to main content

Minimize your nodejs docker image size with these hacks

Note: If you are developing and deploying applications using node.js and docker, this is a post you must not miss.

Docker images have layered filesystems which the docker engine automatically reuses from other images if they are similar. So, in order to even save more filesize from your images, you can follow these steps:

1. Use alpine images

Docker images usually come with an alpine image as well. Alpine images are stripped down linux kernel which has nothing but a barebones OS container in it. I would be writing more about the alpine linux in a future post. But the images of alpine linux do not exceed 5 MB which is a huge life saver when you shipping images to and fro your teams and to production.

Node has a alpine version from its official repository with various versions that are tagged. You can have a look at

Please be aware that they are just plain node and it will not even contain npm. You can install npm within it as required and the steps are given in the readme of the repo.

2. Do an npm install within the image

This is a rookie mistake and I am sure most you wouldn't do an npm install and copy the node_modules folder into the image as well. You write your dockerfile in such a way that the npm install happens inside the image and not copied from your working directory.

3. Do not copy .git directory

This is an easy one. Do not copy the .git directory into your docker image.

4. Use --save-dev while installing development packages

This goes with the above two points and make sure that you use certain development packages only when you are developing and do not install them in your docker image.

5. Use mounted volumes for logs

Do not let the log files be written within the container. Use a mounted volume and separate them out into your host machine.

I have reduced the image by atleast 2x with these steps. Please let me know your inputs if you have got some.

Thanks for your support and appreciation.

You can also follow me at Twitter, Facebook, LinkedIn and Website.


pecundang said…
Nice your Artikel boy.Domino QQ


You are in a cold conference room, Do you want to use your mac as a heater?

Imagine that you are in cold conference room and you kind of feeling the cold on your head. What do you do when you are in a serious meeting and just can't get out?

If you have a MacBook, you might have a solution.

Use your terminal to run either of these commands to keep your CPU at above 80% or if you want you can run them both.

$ openssl speed


$ yes > /dev/null

The first command just outputs the ssl cryptography stats and it keeps your cpu busy. The second command just prints the character "y" into IO.

So try it out and let me know how it works! :D

Talk to me on Twitter, Facebook, LinkedIn or Website
You will receive wonderful short stories written by him and inspirational articles once every month.