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.
What Is Node.js?
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
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.
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:
Once added, restart the Docker Daemon.
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.
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.
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.
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:
Now, when you switch back to the terminal where the server is running, you will see these requests in the server logs.
This means that the application is working. You’ve successfully completed testing the server script locally. Now press CTRL-c to stop the test.
We will now move on to building and running the application in Docker.
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.
Next, add the following line to the Dockerfile. This will help Docker to detect which base image to use for the application.
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.
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.
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.
Now that we have the files inside the image, we can execute the command npm install by using the RUN command.
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.
Now, we will run the CMD command to tell Docker what command we want to run.
The final code will look like this.
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.
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.
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.
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.
You can also run the docker images command to see the list of your local images.
Interested to begin a career in DevOps? Enroll now for the DevOps Certification Course. Click to check out the course curriculum.
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.