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 https://hub.docker.com/r/mhart/alpine-node/

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.

Comments

Popular Posts

Update requires a valid UpdateCommand when passed DataRow collection with modified rows

ToolTip and "Cannot access a disposed object" exception

Finding whether its Design Mode or Runtime Mode in VB.NET