How JavaScript Works: dockerizing a Node.js application

Victor Jonah
SessionStack Blog
Published in
7 min readJul 22, 2021

This is post # 37 of the series, dedicated to exploring JavaScript and its building components. In the process of identifying and describing the core elements, we also share some rules of thumb we use when building SessionStack, a JavaScript tool for developers to identify, visualize, and reproduce web app bugs through pixel-perfect session replay.

Docker is one of the tools that has simplified and eased the development of applications amongst teams. The application is being packaged in a container shipped and run giving the developers a better experience during development, testing, and deployment. This container is being created using a Docker image where all the application’s dependencies and runtime information are required to run it.

In recent years, developers have embraced this approach because they do not need to worry about installing all dependencies on their system. What is needed is just Docker on their computer.

This article will detail the steps to follow whenever we want to Dockerize our application with Node.js. With that, we mean to achieve the smooth development experience Docker provides. This article will not focus on explaining what Docker is but we will look at a few reasons why we would want to Dockerize our application.

Prerequisites

  • Familiarity with Node.js and npm.
  • Basic knowledge of Docker.
  • Docker installed on your machine and a Docker Hub account.

If the items from the list above aren’t installed on your machine, then head over to Docker’s website and follow the instructions for installation. If this is your first time trying our Docker, you might need to understand a few new concepts while following the steps. don’t worry much, you will get it.

Step 1 — Setting up a Node(Express) server

Since we will be using Express, use the application generator tool to quickly create an application structure.

Install the generator globally (if not already installed):

Generate application structure:

Switch into the project directory to access the files. You could also name your application as you prefer. In the example, we are naming the application ‘nodeDockerApp’. Your generated application should have the following structure:

Next, we will remove the public and bin folders in our application because our focus will be on the server and not the frontend:

Afterward, we open our app.js file and define our routes, middleware and create the Express server:

Our package.json file should have these dependencies and scripts:

Next, we define the route files. In the routes/users.js file, you can paste the following code:

With all the dependencies and the server file in place, we are ready to run our application.

Start the application:

You will see this on your terminal:

Run this in your terminal:

and you should get the following:

Step 2 — Dockerizing our app

We got our application running at this point and now is the time where we create a Dockerfile to build our Docker image and more.

Before we begin, it is mandatory to note that there are several steps to the process. And the very first step is to create a Dockerfile. This is a blueprint which in turn will produce a Docker image for you when executed. For a better explanation, Docker reads the instructions of our Dockerfile letting Docker know how the Docker image should be built.

So, let’s create a Dockerfile for our Node application.

Run this in your terminal:

Afterward, add the following instructions to the Dockerfile:

That’s it. Let’s go through each line of our Dockerfile to understand the instructions we gave to Docker:

Here, we are telling Docker to pull in the image from Dockerhub. This image contains all application code and tools needed to make our application run. After the image is run, it becomes a container.

The first line makes a directory for us in the container while the second line makes it our working directory. This means that the container will be working only in this directory.

COPY package.json /usr/src/nodedockerapp

RUN npm install

COPY is copying whatever we want into the WORKDIR (working directory). We have the RUN npm install which installs all dependencies in the container using the package.json:

# Push in app source

COPY . /usr/src/nodedockerapp

We copy our main directory(all files) into the container’s directory.

Step 3 — Ignoring files in the container

Just as we would not want some files accessible or available in Github, we won’t want the same in our container. That’s why we have `.dockerignore` in our project’s directory.

.git
node_modules

Also if you will be using git then you will need to ignore the git folder.

Step 4 — Building your image

Our final step will be to build and then run our image so we get a container. Because the act of running a Docker image produces a container.

So, in our main directory which has the Dockerfile file, we run the following code. This one builds the image:

You can list out the available docker images you have built using the command docker images:

Next, we run the image to produce the container. We will also need to map our server port for our local machine to that of the Дocker so when we run our server inside the container our local machine can connect.

The following command does the above:

Our server is now running in the container with port 5000 linked to our local machine:

To test our port, we run the command:

Our application now works fine in our container.

There are also cases where we might need to use docker-compose. Situations, where we would be using this, occurs if we have more than one container for our application.

Docker-compose will help us manage all containers in a way like it is just a single container.

Containerization makes it much easier for products like SessionStack to be available as a service in the cloud but also provides the option for an on-premises deployment.
SessionStack collects behavioral data during customer journeys, allowing you to replay them as a video. This allows you to see how your customers interact with your product to make it better. Some companies might prefer to process and store this data on their own servers, and Docker makes the deployment process much more efficient.

There is a free trial if you’d like to give SessionStack a try.

SessionStack replaying a session

If you are interested in learning more about Docker and Node.js, you can read the following resources:

If you missed the previous chapters of the series, you can find them here:

--

--