How to Use Docker for Microservices Monitoring: A Practical Guide
Monitoring microservices is critical for ensuring performance, availability, and troubleshooting. Docker simplifies this by providing container-level metrics and easy integration with popular monitoring stacks. This guide covers the core setup using cAdvisor, Prometheus, and Grafana — all run as Docker containers.
Start by running cAdvisor, which collects per-container resource usage (CPU, memory, network, disk). Deploy it with a simple Docker command: docker run -d --name=cadvisor -p 8080:8080 --volume=/var/run/docker.sock:/var/run/docker.sock gcr.io/cadvisor/cadvisor. This exposes metrics at http://localhost:8080/metrics.
Step 1: Set Up Prometheus to Scrape Metrics
Prometheus pulls metrics from cAdvisor and your microservices. Create a prometheus.yml configuration file:
scrape_configs:
- job_name: 'cadvisor'
static_configs:
- targets: ['host.docker.internal:8080']
Then run Prometheus: docker run -d --name=prometheus -p 9090:9090 -v $(pwd)/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus.
Step 2: Visualize with Grafana
Grafana provides dashboards for real-time monitoring. Launch it with:
docker run -d --name=grafana -p 3000:3000 grafana/grafana
Login at http://localhost:3000 (admin/admin). Add Prometheus as a data source (URL: http://host.docker.internal:9090). Import the popular “Docker Monitoring” dashboard (ID 10619) for an instant view of your microservices.
Step 3: Add Custom Service Metrics
For deeper insights, instrument your microservices to expose custom metrics (e.g., request latency, error rates) at a /metrics endpoint. Add a new Prometheus scrape target in prometheus.yml:
- job_name: 'my-service'
static_configs:
- targets: ['service-container:9091']
Restart Prometheus to apply changes. Your custom metrics now appear in Grafana.
With this Docker-based monitoring stack, you gain immediate visibility into resource usage, health, and performance — all without complex installations. Start small, then scale as your microservices grow.