From Netflix to LinkedIn, there is one software development technology used by some of the biggest players in the global business - docker Node.js. Ever since its inception in 2009, this technology has been changing the landscape for software development. So what exactly is the reason behind its popularity in such a short time period? Is it really that simple to build web applications in real-time with Node.js? This article will help you understand what exactly Node.js is by creating a simple Node.js application with Docker.

So, let’s get going.

Improve Your Earning Potential Further!

DevOps Engineer Masters ProgramExplore Program
Improve Your Earning Potential Further!

What Is Node.js?

Docker Node.js or simply, Node.js is an open-source software platform used to build scalable server-side and network applications. These Node.js applications are written in JavaScript and can run within this Node.js runtime on Linux, Windows, or Mac OS without changes. It was originally designed keeping in mind real-time and push-based architecture. Nowadays, it is primarily used for non-blocking, event-driven servers like backend API services and traditional websites.

Docker Node.js uses the Google V8 JavaScript to execute code and has its own built-in library for file, socket, and HTTP communication. This allows Node.js to act as a web server on its own without the support of additional software.

Prerequisites

To build an application, you need to have the following things in place:

  • Node.js version 12.18 or later
  • Docker running locally
  • A text editor or IDE to edit your files

Enable BuildKit

BuildKit enables you to build Docker images. On the Docker Desktop, It is enabled by default for all users. If you do not have Docker Desktop installed, you will have to manually enable BuildKit. 

Build_Node.js_1

To set the BuildKit environment variable when running the docker build command, run:

If you want to enable docker BuildKit by default, you need to set the daemon configuration in /etc/docker/daemon.json feature to ‘true’. Once this is set, restart the daemon. In case the daemon.json file doesn’t exist on your system, create a new file called daemon.json and then add the following command to the file:

Build_Node.js_2.

Once added, restart the Docker Daemon.

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

Create an Application Using Node.js

Now that we have enabled BuildKit, let’s create a simple Docker Node.js application. On your local machine, create a directory named node-docker. Now create a simple REST API using the following steps.

Build_Node.js_3.

Once the REST API is created, we can add some code to handle our REST requests. We will use a mock server called ronin.js for this. In your IDE, open the working directory, and on the server.js file, add the following code.

Build_Node.js_4.

The mocking server will listen on port 8000 by default. You can now make POST requests to the root (/) endpoint. Any JSON structure sent to the server will be saved.

You can also make GET requests at the same endpoint and receive JSON objects that you have previously POSTed.

Test the Application

Now let’s test the application and make sure it’s working. Open your terminal and go to the working directory you created.

Build_Node.js_5.

We will POST some JSON to the API and then make a GET request to see that the data has been saved. Open a new terminal and run the following curl commands:

Build_Node.js_6.

Now, when you switch back to the terminal where the server is running, you will see these requests in the server logs.

Build_Node.js_7.

This means that the application is working. You’ve successfully completed testing the server script locally. Now press CTRL-c to stop the test.

Build_Node.js_8.

We will now move on to building and running the application in Docker.

Earn the Most Coveted DevOps Certification!

DevOps Engineer Masters ProgramExplore Program
Earn the Most Coveted DevOps Certification!

Create a Dockerfile for Node.JS

A Dockerfile is a text document containing the instructions to build a Docker image. Docker reads the instructions from the Dockerfile, executes them, and creates a Docker image.

Create a file named ‘Dockerfile’ at the root of your project. Go to your text editor and open this file. 

The first line you can add to your file is a # syntax parser directive. This step is optional, but it will help the Docker builder to know which syntax to use when parsing the Dockerfile. You can use the docker/dockerfile:1 syntax which always points to the latest release of the version 1 syntax.

Build_Node.js_9

Next, add the following line to the Dockerfile. This will help Docker to detect which base image to use for the application.

Build_Node.js_10.

NODE_ENV is an environment variable that specifies the environment of the application. If you set NODE_ENV to production, it will help improve the performance of the application.

Build_Node.js_11

Now let’s create a working directory. This will make things easier when running the other commands. Docker will use this path as the default location for all subsequent commands. Now you don’t have to type out full file paths but use relative paths based on this working directory.

Build_Node.js_12

We will now copy some packages to the working directory. The packages package.json and package-lock.json will help us to run npm install.

Build_Node.js_13.

Now that we have the files inside the image, we can execute the command npm install by using the RUN command.

Build_Node.js_14.

For the next step, let’s add our source code to the image. We will use the COPY command again for this. This command will take all the files located in the current directory and copy them into the image.

Build_Node.js_15.

Now, we will run the CMD command to tell Docker what command we want to run.

Build_Node.js_16.

The final code will look like this.

Build_Node.js_17.

Want a Job at AWS? Find Out What It Takes

Cloud Architect Master's ProgramExplore Program
Want a Job at AWS? Find Out What It Takes

Create a Dockerfile for Node.js

The Dockerfile will refer to the file specified in the instruction to use in the build context. We need to create a .dockerignore file to improve the context load time and add node_modules directory in it.

Build_Node.js_18.

Build Image

Once we have created the Dockerfile, we can proceed to build our image using the docker build command. The build command can take a --tag flag which is used to set the name of the image. In case you do not have a tag, Docker will use “latest” as the default tag. 

You can build your first Docker image with the following commands.

Build_Node.js_19

View Local Images

We can see the list of images on our local machine by using CLI or Docker Desktop.

Run the images command to view the local images using CLI.

Build_Node.js_20.

Tag Images

An image can have multiple tags. Let’s create a second tag for the image to take a look at its layers. Run the following command.

Build_Node.js_21

You can also run the docker images command to see the list of your local images.

Build_Node.js_22.

Interested to begin a career in DevOps? Enroll now for the DevOps Certification Course. Click to check out the course curriculum.

Next Steps

Now you know the basics of Docker Node.js and how to create a Dockerfile to build a Docker image. If you are interested to learn more, you can sign up for the DevOps Engineer course by Simplilearn which will help you prepare for a career in DevOps. You’ll become an expert not just in Docker but also a lot of other DevOps tools such as Git, Jenkins, and lots more. Get started with this course today and get one step closer to becoming a DevOps Engineer.