Welcome! This project shows how to build and run a Node.js application using microservices. Don't worry if you're new to the idea of microservices—I'll walk you through everything. We also use Docker, which helps package everything up neatly so that it's easy to run, no matter where you are.
By the end, you'll see how to split an application into small parts (called services) and manage them using Docker and Docker Compose. This will make your application easier to scale and maintain.
- Overview
- What are Microservices?
- Technologies Used
- How to Set Up the Project
- Understanding the GitHub Actions Workflow
- Breaking Down the Docker Components
- Docker Compose: Bringing Everything Together
- Key Concepts Explained
- Contributing
- License
This project is a basic microservices setup using Node.js, MongoDB, and NGINX. If these words sound unfamiliar, don't worry—I'll break them down in simple terms. The idea here is to build a modular application, where different parts (called services) are independent but work together.
For example:
- Node.js is the app where we write the backend code (like handling user requests).
- MongoDB is the database that stores your application's data.
- NGINX is used to handle incoming traffic and distribute it across the app for better performance.
We use Docker to package everything into containers, which are like small, isolated environments for each part of our app. Docker Compose helps us manage and run these containers together as one system.
A microservice is just a small, independent piece of an application that does one thing really well. In this project:
- Node.js is the service that runs the backend code.
- MongoDB is the service that stores data.
- NGINX helps direct incoming traffic.
With microservices, each part of the app runs independently, which makes it easier to manage, scale, and update. If one part fails or needs to be updated, the rest of the system can keep running.
- Independence: Each service is its own piece. If one service goes down (e.g., MongoDB), the others can still work.
- Scalability: You can scale each service independently. If your Node.js app needs more processing power, you can run multiple copies without affecting MongoDB or NGINX.
- Easy Maintenance: You can update one service without disrupting the entire app.
- Flexibility: Different services can use different programming languages or databases. In this project, we use Node.js for the app and MongoDB for the database.
- Node.js: JavaScript runtime for building the backend service.
- MongoDB: NoSQL database to store and retrieve data.
- NGINX: A reverse proxy to distribute and manage traffic.
- Docker: To package and run the services in isolated containers.
- Docker Compose: To manage and run multiple containers together.
- GitHub Actions: To automate building and pushing the Docker image.
Before you start, make sure you have these installed on your computer:
- Docker
- Docker Compose
-
Clone the repository: Open your terminal and run:
git clone https://github.com/yourusername/your-repo.git cd your-repo -
Set up environment variables: Create a
.envfile in the root directory and add your environment variables:MONGO_INITDB_ROOT_USERNAME=yourMongoUsername MONGO_INITDB_ROOT_PASSWORD=yourMongoPassword MONGO_DB_USERNAME=yourMongoAppUsername MONGO_DB_PASSWORD=yourMongoAppPassword DOCKER_HUB_USERNAME=yourDockerHubUsername DOCKER_HUB_TOKEN=yourDockerHubAccessToken
These variables allow you to securely pass sensitive data (like passwords) to your services without hardcoding them in your app.
-
Build and run the project: To start all the services, run:
docker-compose up --build
This command will:
- Build the Docker images.
- Start the MongoDB service.
- Start the Node.js app.
- Start NGINX to route traffic.
-
Access the services:
- Mongo Express (MongoDB GUI):
http://localhost:8081 - NGINX:
http://localhost:80
- Mongo Express (MongoDB GUI):
This repository uses GitHub Actions to automate the process of building and pushing the Docker image to Docker Hub.
Whenever you push changes to the master branch:
- GitHub Actions pulls the latest code.
- It builds the Node.js app into a Docker image.
- The image is then pushed to Docker Hub using your Docker Hub credentials.
This keeps your image up to date with every code change and makes it easy to deploy anywhere Docker is supported.
Your Node.js app is packaged into a Docker image. The Dockerfile in the project tells Docker how to set up the environment, install dependencies, and run the app.
MongoDB runs as a separate service inside its own container. This service stores all the data for your app. Since it runs independently from the Node.js app, they can communicate over a network, which means MongoDB can be swapped out or scaled without affecting the app.
NGINX is set up as a reverse proxy. It listens for incoming traffic and directs it to the appropriate service—in this case, your Node.js app. This makes your app scalable and efficient, especially if you’re running multiple instances of the Node.js service.
Docker Compose is a tool that helps you manage multiple Docker containers. The docker-compose.yml file describes all the services that make up your app and how they should interact.
- MongoDB: A NoSQL database to store your app's data.
- Mongo Express: A GUI for managing your MongoDB.
- Node.js app: The main service that handles all backend logic.
- NGINX: The reverse proxy that directs incoming traffic.
You can easily start, stop, and scale all these services with just a few commands using Docker Compose.
Each part of your app (Node.js, MongoDB, etc.) runs in its own container. This means they are isolated from one another. If one service fails, the others can keep running.
With microservices, you can scale individual services based on the load. For example, if your Node.js app needs more processing power, you can run multiple instances of it, while MongoDB and NGINX remain unaffected.
NGINX acts as a reverse proxy that sits in front of your services. It takes incoming requests and forwards them to the right service (in this case, the Node.js app). If you're running multiple copies of the Node.js app, NGINX can also balance the load between them.
Environment variables store sensitive data (like database usernames and passwords) outside the code. This keeps your credentials safe and makes your app easier to configure in different environments.
We welcome contributions! If you have any ideas or spot any issues, feel free to fork this repository, make changes, and submit a pull request.
This project is licensed under the MIT License - see the LICENSE file for details.