Dockerizing NodeJS, Express, and MongoDB App with NGINX as a Reverse Proxy
Tags: node, webdev, docker, nginx
Published: 2022-12-18 04:55:13

🙂 As a beginner developer or while learning Backend development, The most common start point of most developers is building the APIs and connecting the Database with it.

Like If we talk about MERN stack, we can build cool Web-apps using NodeJS and MongoDB as backend frameworks and database respectively. We can even build complex websites like E-Commerce.

🎯 But when it comes to the scalability, we often skip this and we do deploy straightway. Today we will talk about

- How to Dockerize your NodeJS app that is using MongoDB as its Database (Follow [Docker tutorial](https://dev.to/lovepreetsingh/what-is-docker-and-how-it-works-devops-424n) incase you don't know)
- How to use NGINX as load balancer or reverse proxy to balance the load (Follow [NGINX tutorial](https://dev.to/lovepreetsingh/nginx-advanced-load-balancer-web-server-reverse-proxy-4i23) before)
- How to use Docker Compose (Follow simple [Tutorial](https://lovepreet.hashnode.dev/dockerize-your-nodejs-app) of How to Dockerize a simple NodeJS app)
- In conclusion, we will see:- How to run a NodeJS app, that is also using Databases connectivity, inside the Docker Containers and Adding NGINX for load balancing for future (If needed)

🔥 Using these things you can build an App that can handle multiple concurrent users. And you can test your App too using JMeter ([Performance Testing](https://lovepreet.hashnode.dev/make-multiple-requests-to-your-server-jmeter)).

😌 Now, Let's start:-

![Docker](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gdanr461rqixkflkf90n.png)

🫵 This is How our App Setup will look like:-

![NodeJS and Nginx with Docker](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5peact70d0owkb2avrax.png)

_As an end user you'll see the IP of NGINX while making any requests. But in our case both NGINX container and NodeJS container will be running on the same IP._

**Docker Compose:-** It is a tool for running multi-container applications on Docker defined using the Compose file format. A Compose file is used to define how one or more containers that make up your application are configured.

🧘 This is how we'll be doing this:-

![Docker Image and Docker Compose](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2dmav71tz66ig0l6psay.png)

🙂 [Here](https://github.com/AlphaDecodeX/NodeJSDockerNginx), You can find the code of our NodeJS app (Using MongoDB as a Database) and Nginx as a reverse proxy.

You can clone the above repo and can see along the implementation.

👉 Our Directory Structure looks Like this:-

nginx
    |
    |
     -> default.conf (Config file to setup NGINX)
     -> dockerfile (Docker File to make docker image and so containers)

nodedocker_app
    |
    |
     -> models (Schema of the items to be stored in the DB)
     -> views (Contains HTML file that will be served when we will open the server)
     -> dockerfile (Docker File to make docker image)
     -> index.js (Entry Point of our code)
     -> package-lock.json
     -> package.json (Dependencies for Npm)
     -> Readme.md

docker-compose.yml

![Docker Compose File](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/shs5b7kc0gvaihnnwvuo.png)

🔥 Here, we have two services in our containers. The first one is our server which is exposing port 3000 and using example-net. (Run docker network create example-net, to create a network that our app will use).

🔥 And the second one is MongoDB which is using a predefined image from the docker and exposing port 27017 (This port has been used by our index.js file to connect to mongoDB).

![Nginx docker container](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ctsjw94bjjg2ty1ncvih.png)

The third service is Nginx, which is running on default 80 Port. 

😌 Please follow the code in more detail to know more about it. 

Note:- In the config file of Nginx we have used ```proxy_pass http://nodejsserver:3000;``` where nodejsserver is the hostname given in the docker-compose file.

🧘 Now, in the terminal run ```docker-compose up``` to build all the images and to run the services inside the containers. Make sure, Docker is running and MongoDB service is also up on your machine.

Now, Go to localhost with default port 80 in your Browser. You will get a Webpage like this:- (Request on 80 port will go through the Nginx by default).

![Web Page Open](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ke3nwg4cjj0roh1h3up8.png)

🎉 WoHoo, we have completed the setup. Now, Migrate your Projects over to Docker and Nginx. Follow for more Interesting tutorials.

![Bugs in Nginx](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d82l88hazdnj7u9p57ki.png)

Comment if you are facing any 1 or 2 Bugs. 😂