🙂 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:-
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:-
🙂 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:-
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
🔥 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).
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
where nodejsserver is the hostname given in the docker-compose file.
http://nodejsserver:3000;
🧘 Now, in the terminal run docker-compose
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.
up
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).
🎉 WoHoo, we have completed the setup. Now, Migrate your Projects over to Docker and Nginx. Follow for more Interesting tutorials.
Comment if you are facing any 1 or 2 Bugs. 😂
Top comments (16)
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.
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.
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.
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..
Haters will hate, amazing post
And fanatics won't listen.
Yes, MongoDB does have transactions, is fast, convenient, and there are other applications than e-commerce.
github.com/alec-c4/kickstart-devop... check this repo
Great blog.
Thanks 😊
Once I ran this application using docker-compose, and hit localhost to see the view, but it retuns me the following:
Can you please guilde what is wrong?
Thanks for sharing this because i was thinking about it.well done!!!
Thanks a lot for amazing content!!
Nice 👍
Great and simplified content. Keep it up bro👏👏
Very Helpful