This repository contains an example Node.js Express application that uses Mongoose for database interactions and Redis for caching. We will also be using Nginx as a reverse proxy to handle incoming requests and distribute the load among multiple instances of the Node.js application.
Before proceeding, make sure you have the following installed on your system:
- Docker (https://www.docker.com/get-started)
- Docker Compose (https://docs.docker.com/compose/install/)
- Clone the repository:
git clone https://github.com/your-username/your-repo.git
cd your-repo
- Choose the appropriate Docker Compose file based on your environment:
For development:
docker-compose -f docker-compose-dev.yml up --build
For production:
docker-compose -f docker-compose-prod.yml up --build
This will build the necessary Docker images and start the containers for Node.js, Redis, and Nginx based on the selected environment.
The application code is structured as follows:
your-repo/
|- app/
| |- Dockerfile
| |- package.json
| |- server.js
|- nginx/
| |- default.conf
|- docker-compose-dev.yml
|- docker-compose-prod.yml
app/
: Contains the Node.js Express application code.app/Dockerfile
: Dockerfile for the Node.js application.app/package.json
: Contains the Node.js application dependencies.app/server.js
: The main entry point for the Node.js application.nginx/
: Contains the Nginx configuration file.nginx/default.conf
: Nginx configuration file to handle requests.docker-compose-dev.yml
: Docker Compose file for development environment.docker-compose-prod.yml
: Docker Compose file for production environment.
-
Node.js Application: Update the
app/server.js
file andapp/package.json
file as per your application's requirements. You can add your Mongoose models and Redis caching logic in this file. -
Nginx Configuration: Update the
nginx/default.conf
file to customize the Nginx settings according to your application needs. The provided configuration sets up a reverse proxy to forward requests to the Node.js application running on port 3000.
After running the containers successfully, you can access the application in your web browser or via any API client by using the following URL:
http://localhost:80
To stop the application, run the following command in the same directory where the appropriate Docker Compose file is located:
For development:
docker-compose -f docker-compose-dev.yml down
For production:
docker-compose -f docker-compose-prod.yml down
This will stop and remove all the containers, networks, and volumes created by the respective docker-compose up
command.
-
For production deployments, consider using an external data store for Redis, such as Amazon ElastiCache or Redis Labs, to ensure data persistence and scaling capabilities.
-
Secure sensitive data like database credentials, API keys, or session secrets by using environment variables or secrets management solutions like Docker Swarm Secrets or Kubernetes Secrets.
-
Implement monitoring and logging solutions to gain insights into the application's performance and troubleshoot issues effectively.
Congratulations! You have successfully dockerized a Node.js Express application with Mongoose, Redis, and Nginx. Enjoy the benefits of containerization, scalability, and easy deployment!