Running Node JS application using Docker, Keymetrics and PM2


This article is about the open source project that I have recently created so-named Dockerized-Express. The goal of the project is to express JS applications using container technology, real-time monitoring, and load balancing, with minimal setup effort. This is almost always required for all modern web applications. You can find the repository in GitHub by following this linkBefore we start diving deep into the project’s technical parts, I would like to introduce the technologies which have been used to create the project.

Technology units

For container technology I have used Docker. Docker allows you to package an application with all of its dependencies into a standardized unit called a container. You can read more about docker on the official website.

To daemonize with Node.js and run it in the cluster-based environment, I have used PM2 js. PM2 is a production process manager for Node.js applications with a built-in load balancer. It allows you to keep applications alive forever, to reload them without downtime, and to facilitate standard system admin tasks. You can read more about PM2 on the official website.

Finally, for monitoring the app I used Keymetrics. Keymetrics provides monitoring reliability, performance, and errors – hassle-free. 

Setting things up

The application uses express JS to handle routing and more common cases. If we look at the file structure at the root level, we can see two essential files which are:

  1. Dockerfile
  2. Process.json

Dockerfile contains instructions that the Docker engine uses to build images. We use the latest PM2 image as a base image. Then we mention the working directory inside the container, being run using WORKDIR keyword. The next step is environment configuration and moving the source files to the working directory using COPY command. Next, we install packages and specify the port using EXPOSE command. Finally, we run the application using CMD command.

The Process.json file contains instructions for the PM2 module to daemonize the node process and run it in the cluster-based environment. Here we should specify, the entry point of the application, how many instances we want to run on, and in which mode.

The rest is a standard express JS application with router and handlers. For more instructions and to see how to integrate your own Keymetrics account to the app, check out the read me file of the repository.


  • Topics:
  • DevOps

Top Stories

High Five! You just read 2 awesome articles, in row. You may want to subscribe to our blog newsletter for new blog posts.