0
0
Dockerdevops~5 mins

Prometheus for Docker monitoring - Commands & Configuration

Choose your learning style9 modes available
Introduction
Monitoring your Docker containers helps you see how they perform and catch problems early. Prometheus collects and stores metrics from your containers so you can track their health and usage over time.
When you want to track CPU and memory use of your Docker containers to avoid crashes.
When you need to see how many requests your web app container handles per second.
When you want alerts if a container stops responding or uses too much resource.
When you want to visualize container metrics on a dashboard like Grafana.
When you want to keep historical data of container performance for analysis.
Config File - prometheus.yml
prometheus.yml
global:
  scrape_interval: 15s

scrape_configs:
  - job_name: 'docker'
    static_configs:
      - targets: ['docker-exporter:9323']

This file tells Prometheus how often to collect data (every 15 seconds) and where to find the Docker metrics endpoint. The targets field points to the Docker metrics exporter running on your machine.

Commands
Create a Docker network so Prometheus and the Docker metrics exporter can communicate easily.
Terminal
docker network create monitoring-net
Expected OutputExpected
No output (command runs silently)
Run a metrics exporter container that exposes Docker host and container metrics on port 9323 for Prometheus to scrape.
Terminal
docker run -d --name docker-exporter --network monitoring-net -p 9323:9323 prom/node-exporter
Expected OutputExpected
a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0u1v2w3x4y5z6a7b8c9d0e1f2
-d - Run container in background
--network monitoring-net - Connect container to the monitoring network
-p 9323:9323 - Expose port 9323 for Prometheus scraping
Run the Prometheus container with the config file mounted. It listens on port 9090 for you to access the monitoring dashboard.
Terminal
docker run -d --name prometheus --network monitoring-net -p 9090:9090 -v $(pwd)/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus
Expected OutputExpected
f1e2d3c4b5a697887766554433221100ffeeddccbbaa99887766554433221100
-v $(pwd)/prometheus.yml:/etc/prometheus/prometheus.yml - Mount local Prometheus config file into container
-p 9090:9090 - Expose Prometheus web UI port
Check that Prometheus is running and serving metrics on its web interface.
Terminal
curl http://localhost:9090/metrics
Expected OutputExpected
# HELP prometheus_build_info A metric with a constant '1' value labeled by version, revision, and branch from which Prometheus was built. # TYPE prometheus_build_info gauge prometheus_build_info{branch="HEAD",goversion="go1.20",revision="abc123",version="2.44.0"} 1 ...
Verify that both Prometheus and the Docker metrics exporter containers are running.
Terminal
docker ps
Expected OutputExpected
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f1e2d3c4b5a6 prom/prometheus "/bin/prometheus --c…" 10 seconds ago Up 9 seconds 0.0.0.0:9090->9090/tcp prometheus a1b2c3d4e5f6 prom/node-exporter "/node_exporter" 20 seconds ago Up 19 seconds 0.0.0.0:9323->9323/tcp docker-exporter
Key Concept

If you remember nothing else from this pattern, remember: Prometheus collects metrics by regularly scraping endpoints exposed by exporters running alongside your Docker containers.

Common Mistakes
Not exposing the metrics port (9323) on the exporter container.
Prometheus cannot reach the metrics data without the port being open, so no data is collected.
Always use the -p flag to expose the exporter port, like -p 9323:9323.
Mounting the Prometheus config file incorrectly or with wrong path.
Prometheus will fail to start or use default config, missing your Docker targets.
Use an absolute or correct relative path and mount it to /etc/prometheus/prometheus.yml inside the container.
Not connecting Prometheus and exporter containers to the same Docker network.
They cannot communicate by container name or IP, so scraping fails.
Create and use a shared Docker network for both containers.
Summary
Create a Docker network to connect Prometheus and the metrics exporter.
Run a Docker metrics exporter container exposing metrics on port 9323.
Run Prometheus container with a config file pointing to the exporter endpoint.
Verify Prometheus is running and scraping metrics by accessing its web UI or metrics endpoint.
Check running containers to ensure both Prometheus and exporter are active.