Containers can appear, disappear, and move, so a fixed list of servers is not enough to monitor an application. Prometheus runs in a container and collects numerical metrics from application containers and the Docker Engine. Its web UI lets you find metrics and query them; Grafana can turn those queries into dashboards. section 9.1 section 9.4
To get metrics flowing:
- Expose them. Add a Prometheus client library and a metrics HTTP endpoint to each application component. Clients can report runtime measurements; application code can also record custom counters and gauges. section 9.2
- Configure scraping. Prometheus pulls metrics from named endpoints on a schedule. In Docker Compose, targets can use service names resolved through Docker DNS. section 9.3
- Check the results. Open the application’s metrics endpoint, then use the Prometheus UI to find measurements and run queries before adding them to a Grafana dashboard. section 9.2 section 9.4
Listing 9.2 Configuring Prometheus to scrape application metrics
global:
scrape_interval: 10s
scrape_configs:
- job_name: "image-gallery"
metrics_path: /metrics
static_configs:
- targets: ["image-gallery"]
- job_name: "iotd-api"
metrics_path: /actuator/prometheus
static_configs:
- targets: ["iotd"]
- job_name: "access-log"
metrics_path: /metrics
dns_sd_configs:
- names:
- accesslog
type: A
port: 80
This sample configuration scrapes three services every 10 seconds. It shows both static targets and DNS service discovery, plus a component whose metrics use /actuator/prometheus rather than /metrics. section 9.3
Count requests across containers
Scale the access-log service to three containers and send requests to the application. Query `sum(access_log_total) without(hostname, instance)` to see their combined count. For scaled services, use DNS service discovery so Prometheus scrapes every container: a static target resolves only one address and can give misleading results. section 9.3
For hands-on practice, run an application with Prometheus and Grafana in Docker Compose. Configure scraping, then build a dashboard for tasks created, total HTTP requests, and requests currently in progress. section 9.6







