This guide provides step-by-step instructions for setting up Docker container monitoring on AWS EC2 using Prometheus, Node Exporter, cAdvisor, and Grafana. We'll also include a Flask application deployment for demonstration purposes.
Prerequisites
AWS account with EC2 access
Basic understanding of AWS EC2
SSH client installed on your local machine
Step 1: Setting up the EC2 Instance
Launch a new EC2 instance with these specifications:
Instance type: t2.medium (minimum recommended)
Operating System: Ubuntu Server 20.04 LTS
Storage: At least 20GB EBS
Configure Security Group with the following ports:
SSH (22) Flask App (5000) Grafana (3000) Prometheus (9090) Node Exporter (9100) cAdvisor (8080)
Step 2: Docker Installation
You have two options for installing Docker:
Option 1: Manual Installation
SSH into your instance and run the commands manually.
Option 2: Automated Installation (Recommended)
Use the following script in EC2 user data:
#!/bin/bash
# Update system packages
sudo apt-get update
sudo apt-get upgrade -y
# Install required packages
sudo apt-get install -y \
apt-transport-https \
ca-certificates \
curl \
software-properties-common \
git
# Add Docker's official GPG key
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
# Add Docker repository
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
# Install Docker
sudo apt-get update
sudo apt-get install -y docker-ce docker-ce-cli containerd.io
# Configure Docker
sudo systemctl start docker
sudo systemctl enable docker
sudo usermod -aG docker ubuntu
# Install Docker Compose
sudo curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
Step 3: Verify Docker Installation
ssh -i your-key.pem ubuntu@your-ec2-ip
docker --version
docker-compose --version
Step 4: Deploy Flask Application
- Clone your Flask application repository:
git clone https://github.com/SlayerK15/Task-Manager.git
cd Task-Manager
- Start the Flask application containers:
docker-compose up -d
Step 5: Set Up Monitoring Stack
- Create and configure monitoring directory:
mkdir monitoring
cd monitoring
- Create docker-compose.yml:
version: '3'
services:
prometheus:
image: prom/prometheus
ports:
- "9090:9090"
volumes:
- ./prometheus.yml:/etc/prometheus/prometheus.yml
command:
- '--config.file=/etc/prometheus/prometheus.yml'
node-exporter:
image: prom/node-exporter
ports:
- "9100:9100"
cadvisor:
image: gcr.io/cadvisor/cadvisor:latest
ports:
- "8080:8080"
volumes:
- /:/rootfs:ro
- /var/run:/var/run:ro
- /sys:/sys:ro
- /var/lib/docker/:/var/lib/docker:ro
- /dev/disk/:/dev/disk:ro
grafana:
image: grafana/grafana
ports:
- "3000:3000"
environment:
- GF_SECURITY_ADMIN_PASSWORD=admin
- GF_SECURITY_ADMIN_USER=admin
depends_on:
- prometheus
- Create prometheus.yml:
global:
scrape_interval: 15s
scrape_configs:
- job_name: "node-exporter"
static_configs:
- targets: ["node-exporter:9100"]
- job_name: "cadvisor"
static_configs:
- targets: ["cadvisor:8080"]
-
Start the monitoring stack:
docker-compose up -d
Step 6: Verify Services
Check if all services are accessible at:
Grafana: http://your-ec2-ip:3000
Flask App: http://your-ec2-ip:5000
cAdvisor: http://your-ec2-ip:8080
Node Exporter: http://your-ec2-ip:9100
Prometheus: http://your-ec2-ip:9090/targets
Step 7: Configure Grafana
Login to Grafana:
Default credentials: admin/admin
Change password when prompted
Add Prometheus Data Source:
Click "Configuration" → "Data Sources"
Click "Add data source"
Select "Prometheus"
Set URL to "http://prometheus:9090"
Click "Save & Test"
Import Dashboard:
Click the "+" icon → "Import"
Upload the provided JSON dashboard file
Select your Prometheus data source
Click "Import"
get the dashboard form here : dashboard.json
Dashboard Details
The imported dashboard includes:
Disk I/O Usage
Container Network Traffic
Memory Usage
Network Statistics
CPU Usage
Each metric is displayed in a time-series format with the following features:
Auto-refresh every 15 seconds
Last 15 minutes of data by default
Color-coded thresholds for easy monitoring
Container-specific filtering
Troubleshooting
- If containers fail to start:
docker-compose logs
- If metrics aren't showing:
Check Prometheus targets: http://your-ec2-ip:9090/targets
Verify container logs:
docker-compose logs prometheus
- If Grafana can't connect to Prometheus:
Verify network connectivity between containers
Check Prometheus is running:
docker ps | grep prometheus
Security Considerations
Change default Grafana credentials immediately
Use AWS Security Groups to restrict access to necessary ports
Consider implementing HTTPS for all services
Regularly update container images
Maintenance
- Regular backups:
docker-compose exec grafana backup
- Update containers:
docker-compose pull
docker-compose up -d
- Monitor disk usage:
df -h
This setup provides a robust monitoring solution for Docker containers, with real-time metrics and customizable dashboards. The included Grafana dashboard offers comprehensive visibility into container performance and resource usage.