Dockerizing a Node.js app

Bringing to the end our series of Docker blogs, it remains for us to mention Dockerization, and by using our example show you a useful tutorial that can help you in Dockerization of more complex applications.

So, what does Dockerization stand for?

Dockerization is the process of packing, deploying, and running applications using Docker containers. ‌In other words, Dockerization lets you bundle up your software along with all its dependencies in a self-contained package so that it can be run without going through a troublesome setup process. Docker is fast, so you can Dockerize your application in minutes.

Example of Dockerizing Node.js app

Before we start, we recommend you to review the commands for dockerizing the application. You can check the commands here.

Now, let’s get a look at the structure of our application, basically what we will dockerize, a simple Node.js application where we have created a way to start the server with one endpoint.

After that, let’s see the content of the index.js file, which is basically the file where the HTTP server is implemented. The Port of the application is set up either using the env variable or is by default set to 3322.

We have one endpoint ‘liveness’ which can later on be used by some docker orchestration tool for status of our service. Your operating system emits events to your Node.js process, too, depending on the circumstances occurring outside of your program. This gives the Node.js application an opportunity to terminate gracefully.

For example, by no longer accepting new connections, stopping persistent connections, completing outstanding requests, and finally closing files and connection to databases, and exiting.

In our setup we are handling SIGTERM signal by closing our server for any new requests and finally closing the whole application.

Let’s dive into the most important file of this article Dockerfile. Command FROM will define our base image from which we will build our own image. Some of the best practice tips for base image:

  • Always set version of base image
  • Keeping your images size small can be achieved by using -alpine versions

Do not run your container as root for security reasons, official Node image and all -alpine versions come with the least privileged user node, which can be used. To fully support the build process it is not enough to just use node since it will just ensure only process is owned by USER, we need to set permissions on all copied files as well.

The EXPOSE instruction exposes the specified port and makes it available only for inter-container communication. This will not allow access from the host machine. To access from the host machine we need to map container port to host machine port.

To be able to properly handle events and be able to gracefully shut down our service we will not use npm or .sh scripts to start our service.

The order of commands in Dockerfile is important because it can make the build process shorter or longer. Each command creates a layer of cache which will be reused in each new build if it’s not changed. If one layer has changed Docker all other layers after it will not be used and the build process will re-run all commands after changing one.

In order to skip some files being dockerized for security reasons or any other we need to set up .dockerignore and specify which files or folders should be skipped. In our example node_modules will not be needed since npm install is defined in Dockerfile and we will pass our environment variable in the run command.

With all things set up, we can start building our first image. Run the next command at the root of the project. The image is now built and stored in our local image repository.

docker build. -t waltercode/docker-nodejs:v01

To run our docker image on a local machine.

Docker run -p 3322:3322 waltercode/docker-nodejs:v01

After it’s started using the command docker ps in the terminal we should see our docker container.

If we want to make our image available for public usage we can push the build image to one of the Docker repositories (the most popular DockerHub).

docker push waltercode/docker-nodejs:v01.

Conclusion

In this module, we looked at setting up our example Node application that you can use to continue your Docker learning. This example aims to demonstrate you how to get Node.js application into a Docker container. We also created a Dockerfile that we used to build our Docker image.

After reading this article, you should be armed with enough knowledge to Dockerize your own applications, even if they’re built with some other technology. This completes our Docker series, and we hope that we’ve spread some valuable knowledge.

We frequently cover interesting topics from the world of IT, so make sure to follow our social media for the latest updates!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store