This Banner is For Sale !!
Get your ad here for a week in 20$ only and get upto 15k traffic Daily!!!

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


πŸ™‚ As a newbie developer or whereas studying Backend growth, The commonest begin level of most builders is constructing the APIs and connecting the Database with it.

Like If we speak about MERN stack, we are able to construct cool Net-apps utilizing NodeJS and MongoDB as backend frameworks and database respectively. We are able to even construct complicated web sites like E-Commerce.

🎯 However in terms of the scalability, we frequently skip this and we do deploy straightway. At the moment we’ll speak about

  • Methods to Dockerize your NodeJS app that’s utilizing MongoDB as its Database (Observe Docker tutorial incase you do not know)
  • Methods to use NGINX as load balancer or reverse proxy to stability the load (Observe NGINX tutorial earlier than)
  • Methods to use Docker Compose (Observe easy Tutorial of Methods to Dockerize a easy NodeJS app)
  • In conclusion, we’ll see:- Methods to run a NodeJS app, that can also be utilizing Databases connectivity, contained in the Docker Containers and Including NGINX for load balancing for future (If wanted)

πŸ”₯ Utilizing these items you possibly can construct an App that may deal with a number of concurrent customers. And you’ll check your App too utilizing JMeter (Performance Testing).

😌 Now, Let’s begin:-

Docker

🫡 That is How our App Setup will seem like:-

NodeJS and Nginx with Docker

As an finish person you will see the IP of NGINX whereas making any requests. However in our case each NGINX container and NodeJS container will likely be working on the identical IP.

Docker Compose:- It’s a instrument for working multi-container functions on Docker outlined utilizing the Compose file format. A Compose file is used to outline how a number of containers that make up your utility are configured.

🧘 That is how we’ll be doing this:-

Docker Image and Docker Compose

πŸ™‚ Here, Yow will discover the code of our NodeJS app (Utilizing MongoDB as a Database) and Nginx as a reverse proxy.

You may clone the above repo and may see alongside the implementation.

πŸ‘‰ Our Listing Construction appears Like this:-

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

nodedocker_app
|
|
-> fashions (Schema of the gadgets to be saved within the DB)
-> views (Accommodates HTML file that will likely be served after we will open the server)
-> dockerfile (Docker File to make docker picture)
-> index.js (Entry Level of our code)
-> package-lock.json
-> bundle.json (Dependencies for Npm)
-> Readme.md

docker-compose.yml

Docker Compose File

πŸ”₯ Right here, we now have two providers in our containers. The primary one is our server which is exposing port 3000 and utilizing example-net. (Run docker community create example-net, to create a community that our app will use).

πŸ”₯ And the second is MongoDB which is utilizing a predefined picture from the docker and exposing port 27017 (This port has been utilized by our index.js file to hook up with mongoDB).

Nginx docker container

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

😌 Please comply with the code in additional element to know extra about it.

Notice:- Within the config file of Nginx we now have used

proxy_pass http://nodejsserver:3000;

the place nodejsserver is the hostname given within the docker-compose file.

🧘 Now, within the terminal run

docker-compose up

to construct all the pictures and to run the providers contained in the containers. Be certain, Docker is working and MongoDB service can also be up in your machine.

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

Web Page Open

πŸŽ‰ WoHoo, we now have accomplished the setup. Now, Migrate your Tasks over to Docker and Nginx. Observe for extra Fascinating tutorials.

Bugs in Nginx

Remark if you’re dealing with any 1 or 2 Bugs. πŸ˜‚

The Article was Inspired from tech community site.
Contact us if this is inspired from your article and we will give you credit for it for serving the community.

This Banner is For Sale !!
Get your ad here for a week in 20$ only and get upto 10k Tech related traffic daily !!!

Leave a Reply

Your email address will not be published. Required fields are marked *

Want to Contribute to us or want to have 15k+ Audience read your Article ? Or Just want to make a strong Backlink?