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


Installation of rvm erroring out in Catalina - Ruby 2.3.x - How to solve them?

If you had recently updated your macOS to Catalina and then faced problem with installing certain versions of ruby because of random error, pls try the following.
If you are getting the following error, while doing a rvm install
Error running '__rvm_make -j8',
Try the following:
Possiblility is that because of openssl 1.1. OpenSSL 1.1 is not compatible with the ruby compilation during the rvm install.
First install openssl 1.0 using macports
$ sudo port install openssl10
Now install (reinstall) the ruby using rvm with openssl pointing to the macports version
$ PKG_CONFIG_PATH=/opt/local/lib/openssl-1.0/pkgconfig rvm reinstall 2.3.7 --with-openssl-lib=/opt/local/lib/openssl-1.0 --with-openssl-include=/opt/local/include/openssl-1.0
Just ensure that the openssl from macports is installed into /opt/local/lib/openssl-1.0
Hope this helps.
Talk to me on TwitterFacebookLinkedIn or Website