This project demonstrates how to deploy and monitor a full-stack application using Docker, Prometheus, Grafana, Loki, and Nginx Proxy Manager on a custom domain. It features a FastAPI backend and ReactJS frontend, containerized with Docker Compose, deployed on AWS EC2, and enhanced with observability tools and secure traffic management.
- Objective: Containerize and deploy a full-stack application with real-time monitoring and secure access via a custom domain.
- Tech Stack:
- Backend: FastAPI
- Frontend: ReactJS
- Containerization: Docker, Docker Compose
- Monitoring: Prometheus, Grafana, Loki, Promtail, cAdvisor
- Traffic Management: Nginx Proxy Manager
- Database: PostgreSQL with Adminer
- Deployment: AWS EC2
- Security: SSL certificates via Let’s Encrypt
- Launch an EC2 instance and configure security groups for ports (e.g., 80, 443, 3000, 5432, 8000, 8080, 9090).
- SSH into the instance and clone the repository:
git clone https://github.com/The-DevOps-Dojo/cv-challenge01.git cd cv-challenge01 - Optionally, connect VSCode to your EC2 instance for easier editing.
- Backend (FastAPI):
- Navigate to
backend/and create aDockerfile
- Navigate to
- Frontend (ReactJS):
- Navigate to
frontend/and create aDockerfile
- Navigate to
- Docker Compose:
- Create
compose.ymlin the root directory to define services:backend,frontend,db,adminer, andproxy_manager
- Create
- Create a
monitoring/folder and add:prometheus.yml: Configures Prometheus scraping.loki-config.yml: Sets up Loki log storage.promtail-config.yml: Configures Promtail log scraping.
- Create
compose.monitoring.ymlto define monitoring services:prometheus,loki,promtail,grafana, andcadvisor - Install the Loki Docker driver:
docker plugin install grafana/loki-docker-driver:2.9.1 --alias loki --grant-all-permissions
- Edit
/etc/docker/daemon.jsonto use the Loki log driver:{ "log-driver": "loki", "log-opts": { "loki-url": "http://localhost:3100/loki/api/v1/push", "loki-batch-size": "400" } }
- Backend: Update
backend/.envwith your EC2 IP and database credentials. - Frontend: Update
frontend/.envwith your EC2 IP.
- Start the application:
docker compose up -d
- Verify containers are running:
docker ps -a
- Check logs if issues arise:
docker logs <container_id>
- Access services via browser (e.g.,
http://<EC2_IP>:5173for frontend).
- Obtain subdomains (e.g., from AfraidDNS) and point them to your EC2 IP.
- Access Nginx Proxy Manager UI (
<EC2_IP>:81, default creds:admin@example.com/changeme). - Generate SSL certificates for each subdomain in the UI.
- Create
Nginx/nginx.confwith routing rules - Update
compose.ymlto mountnginx.conf:volumes: - ./Nginx/nginx.conf:/data/nginx/custom/http_top.conf
- Update
backend/.envandfrontend/.envwith your domain and setEnvironment=production. - Restart the application:
docker compose down docker compose up -d
- Log into Grafana (
<domain>/grafanaor<EC2_IP>:3000). - Add data sources:
- Prometheus:
http://prometheus:9090/prometheus - Loki:
http://loki:3100
- Prometheus:
- Import container dashboard:
- Use ID
19792, select Prometheus, and load.
- Use ID
- Create logs dashboard:
- Add visualization with Loki, filter by
job=varlogs, and switch to table view.
- Add visualization with Loki, filter by
- Test subdomains (e.g.,
harbeyy.mooo.com,db.harbeyy.mooo.com) for proper routing and SSL.
- Scalability: Docker ensures consistent deployment across environments.
- Security: SSL certificates and HTTP-to-HTTPS redirection.
- Observability: Real-time metrics (Prometheus/Grafana) and logs (Loki/Promtail).
- Traffic Management: Nginx Proxy Manager routes requests efficiently.