Complete Guide: Setting up Docker Container Monitoring on AWS EC2

Complete Guide: Setting up Docker Container Monitoring on AWS EC2

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

  1. 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

  2. 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.

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

  1. Clone your Flask application repository:
git clone https://github.com/SlayerK15/Task-Manager.git
cd Task-Manager
  1. Start the Flask application containers:
docker-compose up -d

Step 5: Set Up Monitoring Stack

  1. Create and configure monitoring directory:
mkdir monitoring
cd monitoring
  1. 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
  1. 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"]
  1. Start the monitoring stack:

docker-compose up -d

Step 6: Verify Services

Check if all services are accessible at:

Step 7: Configure Grafana

  1. Login to Grafana:

  2. Add Prometheus Data Source:

    • Click "Configuration" → "Data Sources"

    • Click "Add data source"

    • Select "Prometheus"

    • Set URL to "http://prometheus:9090"

    • Click "Save & Test"

  3. 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

  1. If containers fail to start:
docker-compose logs
  1. If metrics aren't showing:
  1. If Grafana can't connect to Prometheus:
  • Verify network connectivity between containers

  • Check Prometheus is running: docker ps | grep prometheus

Security Considerations

  1. Change default Grafana credentials immediately

  2. Use AWS Security Groups to restrict access to necessary ports

  3. Consider implementing HTTPS for all services

  4. Regularly update container images

Maintenance

  1. Regular backups:
docker-compose exec grafana backup
  1. Update containers:
docker-compose pull
docker-compose up -d
  1. 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.