How To Set Up Node.js With MongoDB Using Docker

Docker Overview

Docker is an open platform for developing, running, and shipping apps. Docker allows you to separate your apps from your architecture to deliver the software faster. With Docker, you can manage your infrastructure the same way you manage your apps.

By taking advantage of Docker’s methodology for dispatching, testing, and uploading code faster on the server, you can significantly reduce the delay between writing code and running it to production.

Docker includes the components like:

  • Docker client
  • Docker server
  • Docker Machine
  • Docker hub
  • Docker composes, etc.

Why Docker?

Docker is designed to benefit both the Developer and the System Administrator. There are the following reasons for using Docker:

1. Docker allows us to easily install and run the software without having to worry about setting up or dependencies.
2. Developers use Docker to remove machine problems like code working on some developers’ machines and not working on others’ machines when working with the team.
Operators use Docker to run and manage applications on small containers for optimal computer density.
3. Enterprises choose Docker for agile software delivery pipelines to deliver new app features more quickly and securely.

To follow along, you must have docker and node installed.

Today, we will learn how we can deploy two services Node.js app with MongoDB.

Agenda:

🎯 Node project setup
🎯 Docker setup
🎯 Folder structure
🎯 Code breakdown

Initiate The Project

npm init -y

Install the dependencies

npm i express dotenv mongoose


Docker Setup

To pull node and mongo images, go to the docker hub and get the official images. Make sure your docker daemon is on. Click on the docker desktop to activate the daemon. To check if your docker is installed on your machine, in your terminal, run

docker --version

Pull Node Image

Pull Mongo Image

Check The Docker Images

docker image ls

Folder Structure

Here’s our folder structure in its most basic architecture.

 

Code Breakdown

🐋 Database Setup

In this config folder, we imported the mongoose module, connected it to our database, and exported the configuration.

⚠️ Note:

1. The

docker-node-mongo

can be named anything.

2. Notice the

mongo

:27017/docker-node-mongo, this replaces the

localhost

we use in our development mode.

🐋 index.js

const express = require('express');
const dotenv = require('dotenv');
dotenv.config();
const connectDb = require('./config/db');

const app = express();

connectDb();
const port = process.env.NODE_LOCAL_PORT || 3020;

app.use(express.json());
app.use(express.urlencoded({ extended: true }));

app.get('/', (req, res) => {
res.send('Hello World');
});

app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});

Now, let’s test our app locally by running the following command:

npm start

Next, let’s stop the server by running Ctrl + C.

Docker File

 

  • The first line is the base image that we want to use.
  • The WORKDIR command is used to set the working directory for the container.
  • We copy the package.json file to the current directory.
  • We install our dependencies.
  • The COPY command copies the contents of the current directory to the Dockerfile’s context.
  • The EXPOSE command is used to expose ports to the outside world.
  • The CMD directive is used to specify the command that will be run when the image is run.

We could actually go ahead to build this image using this Dockerfile by running the build command:

docker build -t [name of container]

and then running using

docker run -p 8082:8082 [image name] ...

but it will not connect to the MongoDB service. In other to be able to run the two services we need a docker-compose.yml file

Docker Compose File

Lastly, we setup our docker-compose.yml for the app and mongo service. This file helps us to build and link our Node.js app to the mongo image.

Testing

To test we run this command:

docker compose up -d

The -d the flag means we are running the container in a detached mode.

To Check Container Logs

To Check Running Containers

We see the two containers up and running.
Let’s go over to our browser

coma

Conclusion

Setting up a Node.js project with MongoDB using Docker is a great way to manage dependencies, isolate development environments, and ensure consistency across different systems.

By following the steps outlined in this guide, you can create a folder structure, configure Docker, and write the necessary code to get your project up and running.

With the knowledge gained from this guide, you should be well-equipped to create your own Node.js and MongoDB projects using Docker.

Keep Reading

Keep Reading

Leave your competitors behind! Become an EPIC integration pro, and boost your team's efficiency.

Register Here
  • Service
  • Career
  • Let's create something together!

  • We’re looking for the best. Are you in?