Docker: NodeJs and MongoDb Services
Tags: docker, devops, javascript, node
Published: 2022-05-10 08:03:31


Hello there, This post is a continuation of my docker article. To check click the links. 
[Docker in and out](https://dev.to/drsimplegraffiti/docker-ins-and-outs-40hb)
[Dockerize NodeJs app](https://dev.to/drsimplegraffiti/dockerize-nodejs-application-2p3i)
![contain](https://media.giphy.com/media/GJ7ZFaZgwDvGs31vRC/giphy.gif)
To follow along, you must have docker and node installed. 
 

---

Today, we will learn how we can deploy two services `NodeJs app with MongoDb`.

Find the source code [here](https://github.com/drsimplegraffiti/mongodb_docker)

#### Agenda
🎯 Node Project Setup
🎯 Docker setup
🎯 Folder structure
🎯 Code breakdown
 
---


##### Initiate the project
`npm init -y`

---

##### Install the dependencies
`npm i express dotenv mongoose`

---

##### package.json
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dugbfx3uplzrl0z0he61.PNG)
 
---

## Docker Setup
To pull node and mongo image, go to docker hub and get the official images. [click](https://hub.docker.com/)
Make sure your docker deamon is on. Click on the docker desktop to activate the deamon.

To check if your docker is installed on your machine, run `docker --version` in your terminal
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mq7733iyt84mye94bwq9.PNG)
 
---
 
##### Pull node image
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gaglber9yuj5hgwgx0ui.PNG)

---

##### Pull the mongo image
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uxbra76hs63plko39b4b.PNG)

---

##### Check the docker images 
> docker image ls 
 
---

##### Folder structure
Here's our folder structure in its most basic architecture.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eg99kqqyohipd08rm2fl.PNG)

---

### Code breakdown

##### 🐋 Database setup
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ojjxdsqrr8b0gg87u42v.PNG)
In this config folder, we imported the mongoose module, connected 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.

---

##### 🐋 Model and Schema
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oq4jrmo1iphermoxqzuw.PNG)
Here we import the mongoose library and create a new schema for the user.

---

##### 🐋 Routes
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l7v06pn5hwsru3gp2qn0.PNG)
We write the business logic and export the express router and thereafter mount it in the main entry file `app.js`

---

##### 🐋 .env
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7h7x58oq6ta1onjyo3yx.PNG)
 
---

##### 🐋 app.js
```javascript
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.use('/', require('./routes/user'));

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`
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9xctnu0ikg3vq2al7wkc.PNG)

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

---

##### Docker file
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cdgyfr82dm76q8zp3vah.PNG)

> 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 like `docker build -t [name of container]` and then run 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
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jipr06gv53uc2239hgkx.PNG)
Lastly, we setup our docker-compose.yml for the app and mongo service. This file helps us to build and link our NodeJs app to the mongo image.

##### Testing
To test we run this command:
`docker-compose up -d`

The `-d` flag means we are running the container in a detached mode.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zdzn760h7xrajpw5co6i.PNG)

---

##### To check running containers
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ys2x24n2ehiuw12lhsmn.PNG)
  
We see the two containers up and running.

Let's go over to our browser
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0k67dync23dauakribsm.PNG)
 
---

##### Testing the <mark>POST</mark> route.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6t1160l8cntnd4x2o48e.PNG)


---

Lets check our created user using the `docker exec` command.
Check the running container
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qjhevjfy19vzvanfs6er.PNG)
 
Enter into the mongo container
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c7tlkhjfgqwkkmmf427y.PNG)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/futrflcj1yjynnkkci18.PNG)
  
---

#####  Conclusion
I hope this post was helpful. Check the above link for the source code if you get stuck while following along.

---

##### Reference
[Traversy Media, Docker with Mongo](https://www.youtube.com/watch?v=hP77Rua1E0c)