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 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

Featured

Rails Engine - How To Keep Your Engine Migrations Abstracted From Your Host Rails App

Have you ever worked on building a Rails Engine and wanted to keep the models, the migrations and everything inside the engine rather than using a generator to copy paste them into your host Rails App? That's a problem everybody faces one time or the other when building Rails Engines to abstract out your huge Rails App. I have found one such solutions that can be helpful for you to keep them separate. Here is a solution for it.
Problem Statement: Instead of using

    rails g my_engine:install
to copy paste the migrations from engine to your rails app, you want to just keep the migrations inside the Rails Engine and do not bother about it.
Solution:  Add the following lines to your engine, so that your engine's migration files are added to the rails app as well.

Now you can do the following from your rails app to run your migrations as you always do:

rake db:migrate
Hope it helps you! If it does help you, give me a shoutout below!

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