NodeJS Application with Docker

In the last article, we went through the basics of Docker. Like what is a Docker Image, what are containers and how we run a docker image on our system. In this article, we will create a NodeJS application, wrap it inside a Docker container, and run that application on our local machine. We won't be creating any complex application, we are just focused on getting NodeJS work inside a Docker Container.

To begin with, let's set up a NodeJS project first. For that, we need to create a folder. Then inside that folder, we need to create a  package.json  file that will contain all the dependencies of our project. Inside  package.json  we will put the following:


{
    "dependencies":{
        "express":"*"
    },
    "scripts":{
        "start":"node index.js"
    }
}

Now create an  index.js  file which will contain our main server startup and API code. Inside index.js we put the following lines of code:

const express = require('express')
const app = express()

app.get('/cube',(req,res)=>{
    var number = req.query.number
    return res.json({
        value: Math.pow(number,3)
    })
})
app.listen(8080,()=>{
    console.log('Listening on port 8080')
})

Here we are using the express framework, we stated that as dependency is our  package.json . In  index.js  we are importing the express library and creating an app instance of it. Then we create an api  /cube  which will take a number as input and return the cube root of that number. Finally, we specify the server to run on port 8080. 

Next step is to create a Dockerfile. A Dockerfile is basically a text file that contains some settings  and commands that are needed to be executed in order to create a docker image of our project. We just need to create  a file named  Dockerfile  and put the following content inside it.

#Specifiy a base image
FROM node:alpine

#Copy all project files inside container
COPY ./ ./
#Install dependencies
RUN npm install

#Start Up command
CMD ["npm","start"]
 
Let's understand each of these commands one by one. First, we need to specify a base image. The container inside which the image will is in total isolation and doesn't have node or npm installed. So we use a  node:alpine  which is a lightweight node environment already available in docker hub. This will give us the node environment to run our project. Next using the COPY ./ ./ command we copy our project files and folders into the container. Then npm install will install all the dependencies inside   package.json  inside the container. And in the end, we specify the command which will start our server.
 
Now we need to build our image. For this, we open up the terminal inside our project folder and run the command  docker build .  , it will start up the build process and on successful build, we will get an image id. 
 

Now to run the image we have to run the command  docker run <build_id> . The node server will start up on the specified port.


There is one more thing to be taken care of. We can see the server running on port 8080 on our local machine, but if we try to access localhost:8080 we won't be able to do so. The reason for this is the server is running on the container's port number 8080, not on our machine's port 8080. To access this via our machine's port 8080 we need to set up a port mapping. The way we do this is specifying some extra parameters in the docker run command. We run the following command:


So, after executing this command we will be able to access the NodeJS application from localhost:8080.


So, using docker we have containerized a NodeJS application. By this method, we can even develop NodeJS application without NodeJS being installed on the system.
Hope this article has helped you to get an understanding of how NodeJS and Docker work together. If you have any feedback and doubts do mention them in the comments. Stay Safe and Happy Coding!

Post a Comment

0 Comments