Dockerizing Nodejs Mongodb Application

By

In this article, you’ll learn how to build a docker image for running a Nodejs and mongodb application. I’ll first give you a brief idea about docker, then we’ll create a docker image for nodejs mongodb application, and run it locally. At last , we’ll push the docker image to docker hub. Lets start step by step .

A Brief introduction to Docker

Docker is a tool that enables developers to develop, ship, and run applications anywhere . It works on the concept of containers .

Now the question is what is this new stuff Container ? Why should I know about this , what benefits it brings to the developer life.

There are various problems which container addresses , lets have a look at those , which I have felt over the years .

  • I use Linux and the Application works fine in my system , but it doesn’t on Windows
  • You might Have different Version of Nodejs or Your Node JS Version is Outdated .
  • Before running my Node app , i need to install various packages and some of them doesn’t gets installed too , Can we have the application in a full packaged form .

Docker solves all the above problems by providing a lightweight packaged version of your application . Which is highly scalable by default in Production . It includes everything from system architecture to infrastructure which includes code , libraries , binaries which you need to run your application . You just Concentrate on executing a command and run it .

All these packaged together called as Docker Images and the instance on which it basically run is known as Docker Containers.

We understood about a basic description about what docker is. Now lets create a sample application using nodejs and mongodb and Dockerize .

In order to do that , we need to download and install docker in our local machine. In order to do this we need to log into docker-hub account (if you don’t have a dockerId create an account in docker-hub and log into it) then we can download and install docker in to our local system.

NodeJS with Docker: Dockerizing a NodeJS application

1 . Create a Simple Nodejs Project

Now we are going to create a simple NodeJS project or you can clone our nodejs REST boilerplate code from this post.

You can directly down load the code as well

https://github.com/Sanjay007/nodejs-api-mongodb-boilerplate.git

Once you have cloned the app , make sure you have mongo installed and running on your local system. Navigate to the folder and run following commands

$ npm install
$ npm install -g nodemon
$ npm start

You will have the server running on port 9000 and you can access the API using URL http://ocalhost:9000/api-docs/

2. Creating Docker Image For Nodejs Application

Make sure you have docker installed on your system , you can also check docker play ground .

Create a Dockerfile in root of your project folder with the following content .

FROM node:latest
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY package.json /usr/src/app
RUN npm install
COPY . /usr/src/app
EXPOSE 9000
# CMD ['node', 'app.js']
RUN npm install -g nodemon
CMD ["node","server.js"]

Lets have a look at the Dockerfile and understand through each line of the Dockerfile and understand what is done .

FROM : We are trying to create a docker image from an existing image .Here we are trying to use the node image as our base image and create our own image on top of that hence I need to say specify which image using this from tag or keyword in Dockerfile.

RUN :- It is used to execute a specific command in the like the way we do it in console , here we are creating a directory.

WORKDIR :– It is used to specify the current working directory where all the commands and instructions will be executed.

COPY :- Pretty simple as name suggest Used to copy a specific file to a specific location .In this example we are going to copy package to JSON in our working directory.

EXPOSE :- It is used to expose port on which application container is going to listen .

3. Building Docker Image With NodeJS and MongoDB

Now that we have  created the doctor for it’s time to build a docker image .  you can create a docker image using the command.

$ docker build -t app .

You can verify the docker image built just now using command below .

[root@localhost]# docker image ls
REPOSITORY                           TAG                 IMAGE ID            CREATED             SIZE
nodejs-api-mongodb-boilerplate_app   latest              d14b5fcb2db1        12 hours ago        1.32 GB

One of the issues while creating our docker image is we have to create images separately for NodeJS and MongoDB and then we need to connect both .Lets have a look at more simpler way using docker compose.

4. Creating Docker Compose Nodejs and Mongo DB

Now the above configuration is not enough , as we need to connect with database . So what we are going to do now is we are going to create multiple services .

version: "2"
services:
  app:
    container_name: app
    restart: always
    build: .
    ports:
      - "9000:9000"
    links:
      - mongo
  mongo:
    container_name: mongo
    image: mongo
    #volumes:
    #  - ./data:/data/db
    ports:
      - "27017:27017"

This will configure and link different container services and you do not need to run set of big command to run our docker image everytime. Lets go ahead and execute .

[root@localhost]# docker-compose build
mongo uses an image, skipping
Building app
Step 1/9 : FROM node:latest
 ---> c66552d59c4b
Step 2/9 : RUN mkdir -p /usr/src/app
 ---> Using cache
 ---> 8e8e5e99d232
Step 3/9 : WORKDIR /usr/src/app
 ---> Using cache
 ---> d18ad4a0254a
Step 4/9 : COPY package.json /usr/src/app
 ---> Using cache
 ---> 1ab452cf3c84
Step 5/9 : RUN npm install
 ---> Using cache
 ---> 34b7b8d842fa
Step 6/9 : COPY . /usr/src/app
 ---> Using cache
 ---> b1362ab6504f
Step 7/9 : EXPOSE 9000
 ---> Using cache
 ---> b5b27e9f32e2
Step 8/9 : RUN npm install -g nodemon
 ---> Using cache
 ---> 4937234346eb
Step 9/9 : CMD node server.js
 ---> Using cache
 ---> d14b5fcb2db1
Successfully built d14b5fcb2db1

5. Running Docker Compose In Detach Mode

Now the above command with run the containers in non detach mode , if you want to run the containers in detach mode , you need to execute the following command .

If you just want to look at before running in non detach mode

$ docker-compose up

Running in Detach Mode

$ docker-compose up -d 

Now if everything goes fine you can see your server up and running at 9000 .

Conclusion

Congrats !! We have Dockerized our NodeJS application using mongo DB . Let me know if you need any help . We have commented running code in debug mode . You can try enable nodemon in Dockerfile .