Here is a step by step overview of how to use Node js on docker. But first let’s talk a little bit about the Docker. Docker is an open platform for developing, running and shipping applications. With Docker you will be able to separate your applications from your infrastructure which in return helps you deliver the software quickly.

Node js on docker

Also, docker will help you manage your infrastructure in the similar way you manage your applications. You can take advantage of Docker by its features that include testing, shopping and deploying code quickly. By using these features you can reduce the delay between writing code and running it in production.

Also Read: Node js with MySQL: Everything you need to know

Explaining the Node js on docker with an Example:

Node js on docker

Here we will create an example of simple Node.js application. You have to create a directory on your local machine named node-docker and then follow us along with the example to create a simple Rest API.

$ cd [path to your node-docker directory]
$ npm init -y
$ npm install ronin-server ronin-mocks
$ touch server.js

Then you have to add some simple codes to handle the REST requests. For us we will use a mocks server so that we can focus on Dockerizing the application and not use the much of the actual code.

After this all you have to do is open this directory in your favorite IDE and enter the following code into the server .js file.

const ronin     = require( 'ronin-server' )
const mocks     = require( 'ronin-mocks' )
 
const server = ronin.server()
 
server.use( '/', mocks.server( server.Router(), false, true ) )
server.start()

We are using the mocking server and it is called the Ronin.js and will list on port 8000 by default. Here you can easily POST requests to the root (/) and after that endpoint and any JSON structure. You send to the server will be saved in memory. 

Testing the Application:

So, lets start running our application and see if it is working properly. Now again and open your terminal and navigate to your working directory you created.

$ node server.js

Now to test if the application is working properly. We will first POST some json to the API and then make a GET request to see that the data has been saved or not. Now open a new terminal and run the command given below:

$ curl --request POST \
  --url http://localhost:8000/test \
  --header 'content-type: application/json' \
  --data '{
	"msg": "testing"
}'
{"code":"success","payload":[{"msg":"testing","id":"31f23305-f5d0-4b4f-a16f-6f4c8ec93cf1","createDate":"2020-08-28T21:53:07.157Z"}]}

$ curl http://localhost:8000/test
{"code":"success","meta":{"total":1,"count":1},"payload":[{"msg":"testing","id":"31f23305-f5d0-4b4f-a16f-6f4c8ec93cf1","createDate":"2020-08-28T21:53:07.157Z"}]}

Now again switch back to the terminal where our servers is running. You will se the requests given below:

2020-XX-31T16:35:08:4260  INFO: POST /test
2020-XX-31T16:35:21:3560  INFO: GET /test

Creating Dockerfiles for Node.js

Now take a look at how will you create a Dockerfile.

You can say that a A Dockerfile is a text document which contains all the commands that a user can call upon. When the user calls the command line to assemble an image. Docker will read the instructions and execute them one by one and create a Docker image as a result.

Now here we will walk through by creating a Dockerfile for our application. Now you will see in the root of your working directory, and then create a file named Dockerfile and open this file in your text editor.

First thing you have to do is add a line in our Dockerfile that tells your Docker. What is the base of image you would like to use for your particular our application.

FROM node:12.18.1

But the good thing is that you can easily inherit base of image from other images. So, instead of creating your own base image you can easily use the official Node.js. Node.js has tools that already have the packages that you will need to run a Node.js application.

You can create Docker Images in JavaScript, you have to write something like we have given below:
class MyImage extends NodeBaseImage {}

By this you will be able to create a class  MyImage that inherited functionality from the base class NodeBaseImage.

Just like that command you will be able to use the FROM command. Through this command you can tell the Docker to include in our images all the functionality from the node:12.18.1 image.

Now to make this easier when running the rest of your commands you can create a working directory.

Now this will inform the Docker to use this particular path as the default location for the subsequent commands. By using this method you don’t have to type out full file paths but can use relative paths based on the working directory.

WORKDIR /app

After you have downloaded the Node.js the first thing you have to do is install the npm packages. What will it do is insure that you have all its dependencies installed in the node_modulesdirectory where the node runtime will be able to find them.

Now before you run the npm install, you need to get your package.jsonand package-lock.json file into your image. You can use the command  COPY to do this.  COPY commands does two things. The first thing is that it tells Docker what files you would like to copy into the image. The second thing tells Docker where the file want to be copied. You can copy the package.jsonand package-lock.json file into our working directory – /app.

COPY package.json package.json
COPY package-lock.json package-lock.json

Now as you can see we have our package.json files inside the image. Now we will use the  RUNcommand to execute the command npm install.

RUN npm install

Jumping on the next thing we will add source code into the image. We will use the COPY command just like we did with our package.json files above.

COPY . .

The last thing we have to do is tell Docker what command we want to run. When your image is running inside of a container. You can run the CMD command.

CMD [ "node", "server.js" ]

Now, you can see the complete Dockerfile.

FROM node:12.18.1
 
WORKDIR /app
 
COPY package.json package.json
COPY package-lock.json package-lock.json
 
RUN npm install
 
COPY . .
 
CMD [ "node", "server.js" ]

Conclusion:

In this post we talked about Node js on docker. We also learned about creating docker images using the Dockerfile. Tagging our images and managing images. Hope you find this information useful. Thank you for the read.

Categorized in:

Tagged in: