DEV Community

Cover image for Dockerizing NodeJS, Express, and MongoDB App with NGINX as a Reverse Proxy
Lovepreet Singh
Lovepreet Singh

Posted on


Dockerizing NodeJS, Express, and MongoDB App with NGINX as a Reverse Proxy

🙂 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 incase you don't know)
  • How to use NGINX as load balancer or reverse proxy to balance the load (Follow NGINX tutorial before)
  • How to use Docker Compose (Follow simple Tutorial 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).

😌 Now, Let's start:-


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

NodeJS and Nginx with Docker

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

🙂 Here, 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:-

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

-> 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)


Docker Compose File

🔥 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

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

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

Bugs in Nginx

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

Top comments (16)

webjose profile image
José Pablo Ramírez Vargas

Can anyone explain to me why the obsession for MEAN, MERN and derivates? I just can't see an e-commerce on MongoDB. Clearly the problem is solved better with RDBMS.

vaibhav68849256 profile image
Vaibhav Khandare

Heyy Jose yes RDBMS is great choice for ecommerce website but if you go by convenient way mongodb are quite easy and they are easily hostable. Just like if any project can made on javascript it will be eventually created in javascript. Similarly mongodb is more convenient.

webjose profile image
José Pablo Ramírez Vargas

So basically people are so enchanted with MongoDB because it is easy? So people don't really care if an RDBMS runs faster, better, has ACID transactions (which MongoDB does not) and therefore the correct choice for an e-commerce website?

That's just mental, my friend.

Thread Thread
vaibhav68849256 profile image
Vaibhav Khandare

For begineers it wont be much necessuary to learn the core concept for dbms they must keep in mind the concepts but for creating prototype mongodb is great..

Thread Thread
juanpa7 profile image
Juan Pablo Guisasola

Haters will hate, amazing post

Thread Thread
webjose profile image
José Pablo Ramírez Vargas

And fanatics won't listen.

Thread Thread
leonardopintodev profile image
Leonardo Pinto

Yes, MongoDB does have transactions, is fast, convenient, and there are other applications than e-commerce.

alec profile image
Alexey Poimtsev (he/hehe)
asadhanif3188 profile image
|| Asad Hanif ||

Once I ran this application using docker-compose, and hit localhost to see the view, but it retuns me the following:

msg: "No items found"

Can you please guilde what is wrong?

tikul1 profile image
Hardik Parmar

Great blog.

lovepreetsingh profile image
Lovepreet Singh

Thanks 😊

410nidhi profile image
Nidhi Upasani

Thanks a lot for amazing content!!

mezieb profile image
Okoro chimezie bright

Thanks for sharing this because i was thinking about it.well done!!!

vaibhav68849256 profile image
Vaibhav Khandare

Very Helpful

mjsf1234 profile image
mrityunjay saraf

Great and simplified content. Keep it up bro👏👏

light_seekern profile image
Taha Syed

Nice 👍

Super Useful CSS Resources

A collection of 70 hand-picked, web-based tools which are actually useful.
Each will generate pure CSS without the need for JS or any external libraries.