Building Docker NodeJS MongoDB Applications Simplified 101

on Container Architecture, Data Integration, Database Management Systems, Docker, MongoDB, Node js, Tutorials • February 14th, 2022 • Write for Hevo

Docker NodeJS MongoDB

As organizations are shifting towards Cloud-native platforms for developing modern applications and satisfying their requirements. The software now needs to be developed with independent microservices for fast delivery, automation, flexibility, and Continous Integration and Development. This makes it easier for Developers to build, test and deploy modern applications in production.

Modern applications widely use Node.js as a backend language to add complex functionalities in the applications with ease. Also, a NoSQL Database such as MongoDB is widely used by companies to store flexible data in the document-oriented format as it supports JSON format. A flexible web application developed using Nodejs MongoDB requires containerization for scaling it effectively. Docker is a simple to use containerization platform. Docker Nodejs MongoDB applications reduce the workload for Developers.

Docker Nodejs MongoDB applications use Virtual environments as containers to run applications in isolation. In this article, you will learn about the steps to how to create and build Docker Nodejs MongoDB applications by configuring the containers and how the Docker Nodejs MongoDB stack help Developers build, test and deploy applications with ease.

Table of Contents

Prerequisites

  • Node.js installed on the local machine.
  • MongoDB is installed on the local machine.
  • Docker installed on the local machine.
  • A brief knowledge of Nodejs and Docker.

Introduction to MongoDB

MongoDB Logo
Image Source

MongoDB is a NoSQL Database system widely used by companies to develop modern flexible applications. It is an open-source cross-platform document-oriented Database because it does not store information in tabular form. Developers use MongoDB Database because of its high flexibility for storing data from modern applications.

MongoDB is developed by MongoDB Inc, and in commercial use since 2009 that help Developers and organization manage the data flexibly from the applications. It uses JSON-type format documents with optional schemas and helps Developers easily manipulate, access, and manage large datasets of distributed data. 

Key Features of MongoDB

Some of the main features of MongoDB are listed below:

  • Schema-Less: MongoDB is a non-Relational Database that uses documents and collections to store and manage data. Users don’t need a predefined schema.
  • Scalability: MongoDB is easy to scale as per the business requirements because it supports sharding that distributes the large Datasets into smaller chunks using the sharding key.
  • High Performance: MongoDB delivers high performance while querying data as compared to other Databases because of its scalability, indexing, and replication features.

To learn more about MongoDB, click here.

Introduction to NodeJS

NodeJS Logo
Image Source

Node.js is an open-source JavaScript runtime environment that is cross-platform and back-end and runs on the V8 engine. It helps the application execute JavaScript code outside the Browser as JavaScript can only be executed in the browser window. Node.js extends the capability and reach of JavaScript to the machine. It is designed to build scalable and flexible network applications.

Node.js uses asynchronous programming to manage applications and provide a smooth experience. No function in Node.js directly performs I/O, the process never blocks. The only case is when the I/O is performed using synchronous methods of the Node.js standard library. 

Key Features of NodeJS

Some of the main features of Node.js are listed below:

  • Asynchronous: Node.js offers all the libraries as non-blocking or asynchronous which means that it never waits for the API to return data and move to the next execution.
  • Scalable: Node.js can handle multiple concurrent requests efficiently and manages load balancing for all CPU cores using the cluster module.
  • Cross-Platform: Nodejs can be used on various systems such as Windows, Unix, Linux, Mac OS X, and mobile devices.

To know more about Node.js, click here.

Introduction to Docker

Docker Logo
Image Source

Docker is an open-source set of PaaS products that offer OS-level virtualization for developing, shipping, and running applications. It is a containerization software platform that allows developers to test, build, and deploy applications quickly. Developers can package their software in such a way that combines application source code with the dependencies required to run the application in production.

Docker is developed by Docker Inc. in 2013 to simplify and fasten the delivery of distributed applications as organizations have shifted towards Cloud-native applications. Docker made it simpler for Developers to build, deploy and manage containers using simple commands.

Key Features of Docker

Some of the main features of Docker are listed below.

  • Docker Swarm: Docker Swarm enables the use of Docker API as a frontend to let different tools control it. It is a clustering and orchestration tool to make it easier for users to control Docker cluster hosts.
  • Routing Mesh: Routing Mesh is a feature that routes the incoming requests on a registered port to active containers. 
  • Easy Configuration: Users can easily set up and configure the system in a hassle-free manner. As the code deployment time of Docker is less.

To learn more about Docker, click here.

Simplify Data Analysis with Hevo’s No-code Data Pipeline

Hevo Data, a No-code Data Pipeline helps to load data from any data source such as MongoDB, SaaS applications, Cloud Storage, SDK,s, and Streaming Services and simplifies the ETL process. It supports 100+ data sources (including 30+ free data sources) and is a 3-step process by just selecting the data source, providing valid credentials, and choosing the destination. Hevo not only loads the data onto the desired Data Warehouse/destination but also enriches the data and transforms it into an analysis-ready form without having to write a single line of code.

Get Started with Hevo for Free

Its completely automated pipeline offers data to be delivered in real-time without any loss from source to destination. Its fault-tolerant and scalable architecture ensures that the data is handled in a secure, consistent manner with zero data loss and supports different forms of data. The solutions provided are consistent and work with different BI tools as well.

Check out why Hevo is the Best:

  1. Secure: Hevo has a fault-tolerant architecture that ensures that the data is handled in a secure, consistent manner with zero data loss.
  2. Schema Management: Hevo takes away the tedious task of schema management & automatically detects the schema of incoming data and maps it to the destination schema.
  3. Minimal Learning: Hevo, with its simple and interactive UI, is extremely simple for new customers to work on and perform operations.
  4. Hevo Is Built To Scale: As the number of sources and the volume of your data grows, Hevo scales horizontally, handling millions of records per minute with very little latency.
  5. Incremental Data Load: Hevo allows the transfer of data that has been modified in real-time. This ensures efficient utilization of bandwidth on both ends.
  6. Live Support: The Hevo team is available round the clock to extend exceptional support to its customers through chat, E-Mail, and support calls.
  7. Live Monitoring: Hevo allows you to monitor the data flow and check where your data is at a particular point in time.
Sign up here for a 14-Day Free Trial!

Building Docker Nodejs MongoDB Containers

Now that you have understood about MongoDB, Node.js, and Docker. In this section, you will learn how to build a container for Docker Nodejs MongoDB. First, you need a Nodejs application connected with your MongoDB Database and then need to create a Docker container and dockerize the MongoDB Nodejs application. The steps to build the Docker Nodejs MongoDB container are listed below.

Step 1: Creating the MongoDB Nodejs Application

  • Open up the shell or command prompt window.
  • Then install the MongoDB driver for your Nodejs application using the command given below.
npm install mongodb
  • Then create or open your existing Nodejs application. 
  • Now, let’s import MongoDB in the Nodejs application. by using the following command given below.
  • Create a new file of your choice. eg- app.js. 
  • Now, install the MongoDB driver by using the code given below.
var mongoose = require('mongoose');
  • Connect to the Database using the following code given below.
mongoose
	.connect(
	'mongodb://mongo:27017/mydb',
	 { useNewUrlParser: true }
	 )
	 .then(() => console.log('MongoDB Connected'))
	 .catch(err => console.log(err));
  • The name of the database is “mydb“, and in the connection URL, the address is localhost with the default port 27017
  • start the MongoDB Database by using the following command in the new terminal in the same directory.
start mongo
  • You can run the application using the following command in the terminal below.
node app.js
  • You will get the output shown in the image below.
MongoDB Database Created
Image Source: Self

Step 2: Creating a Docker File

  • Create a Docker file in the same project directory with the name “Dockerfile” or any name of your choice. 
  • Here, open the Dockerfile you just created in any code editor you wish.
  • Now. let’s add the layers to the docker file by adding multiple instructions to install dependencies for Docker Nodejs MongoDB.
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 3000
CMD ['node', 'app.js']
  • In the above instructions, “FROM node latest” allows getting node as Base Image.
  • RUN mkdir -p /usr/src/app” creates a new directory for app files.
  • “WORKDIR /usr/src/app” sets the working directory in the container.
  • Then, the package.json file is copied from the source directory to the container directory because it contains all the dependencies of the project.
  • RUN npm install” installs the dependencies into the container.
  • COPY . /usr/src/app” copies the source code of the Docker Nodejs MongoDB application into the contaoner directory.
  • As the application is connected to 3000, “EXPOSE 3000” exposes the container to the given port number.
  • CMD [‘node’, ‘app.js’]” runs the terminal command in the container.

Step 3: Running Docker Nodejs MongoDB Application

  • Now, Docker Compose will be used for defining and running multiple container docker applications.
  • Create the file named “docker-compose.yml” in the same directory as “Dockerfile“. 
  • Open the “docker-compose.yml” and add the following lines to it, given below.
version: "3"
services:
 app:
 container_name: app
 image: Dockerfile
 restart: always
 build: .
 ports:
 - "3000:3000"
 links:
 - mongo
 mongo:
 container_name: mongo
 image: mongo
 volumes:
 - ./data:/data/db
 ports:
 - '27017:27017'
  • The compose file now contains 2 services – “app” and “mongo” of Docker Nodejs MongoDB application.
  • Now, let’s run the Docker Nodejs MongoDB application using the following command given below in the project directory.
docker-compose up
  • To stop the Docker Nodejs MongoDB application use press “CTRL+C” or run the following command in another terminal
docker-compose down

That’s it! You have successfully built the Docker Nodejs MongoDB application.

Conclusion 

In this article, you read about MongoDB, Docker, and Node.js. You also learnt the steps to create and build the Docker Nodejs MongoDB applications for scalability and why it is a vital need for organizations to use the Docker Nodejs MongoDB technology stack for developing their applications. Nodejs offers a huge number of libraries to build your flexible application using MongoDB Database for handling data. To scale up the application, they use Docker because it is easy to configure and use.

Visit our Website to Explore Hevo

MongoDB Database stores valuable business data that can be used to generate insights. Companies need to analyze their business data stored in multiple data sources. The data needs to be loaded to the Data Warehouse to get a holistic view of the data. Hevo Data is a No-code Data Pipeline solution that helps to transfer data from 100+ sources to desired Data Warehouse. It fully automates the process of transforming and transferring data to a destination without writing a single line of code.

Want to take Hevo for a spin? Sign Up here for a 14-day free trial and experience the feature-rich Hevo suite first hand.

Share your experience of learning about using the Docker Nodejs MongoDB application in the comments section below!

No-code Data Pipeline For your Data Warehouse