Bird
Raised Fist0
Dockerdevops~5 mins

Prometheus for Docker monitoring - Commands & Configuration

Choose your learning style10 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.

Practice

(1/5)
1. What is the main role of Prometheus when monitoring Docker containers?
easy
A. To run Docker containers automatically
B. To collect and store metrics from Docker containers
C. To build Docker images from Dockerfiles
D. To replace Docker Engine for container management

Solution

  1. Step 1: Understand Prometheus purpose

    Prometheus is designed to collect and store metrics data from various sources, including Docker containers.
  2. Step 2: Differentiate from other Docker tools

    Prometheus does not run containers, build images, or replace Docker Engine; it focuses on monitoring.
  3. Final Answer:

    To collect and store metrics from Docker containers -> Option B
  4. Quick Check:

    Prometheus = Metrics collection [OK]
Hint: Remember: Prometheus collects data, not runs containers [OK]
Common Mistakes:
  • Confusing Prometheus with Docker Engine
  • Thinking Prometheus builds images
  • Assuming Prometheus runs containers
2. Which Docker command correctly starts cAdvisor to provide metrics for Prometheus?
easy
A. docker run --name cadvisor -p 8080:8080 gcr.io/cadvisor/cadvisor:latest
B. docker run --name cadvisor -p 8080:8080 google/cadvisor:latest
C. docker run --name cadvisor -p 9090:9090 prom/cadvisor:latest
D. docker run --name cadvisor -p 9090:8080 google/cadvisor:stable

Solution

  1. Step 1: Identify correct cAdvisor image and port

    The official cAdvisor image is hosted at gcr.io/cadvisor/cadvisor:latest and listens on port 8080.
  2. Step 2: Check port mapping and image name

    docker run --name cadvisor -p 8080:8080 gcr.io/cadvisor/cadvisor:latest correctly maps port 8080:8080 and uses the official image name.
  3. Final Answer:

    docker run --name cadvisor -p 8080:8080 gcr.io/cadvisor/cadvisor:latest -> Option A
  4. Quick Check:

    cAdvisor image and port = docker run --name cadvisor -p 8080:8080 gcr.io/cadvisor/cadvisor:latest [OK]
Hint: Official cAdvisor image is gcr.io/cadvisor/cadvisor:latest [OK]
Common Mistakes:
  • Using wrong image repository
  • Mapping wrong ports
  • Using outdated or incorrect image tags
3. Given this Prometheus scrape config snippet for Docker monitoring:
scrape_configs:
  - job_name: 'cadvisor'
    static_configs:
      - targets: ['localhost:8080']

What will Prometheus do when started with this config?
medium
A. Fail to start due to invalid config syntax
B. Scrape metrics from Docker Engine API on port 8080
C. Scrape metrics from all Docker containers directly
D. Scrape metrics from cAdvisor running on localhost port 8080

Solution

  1. Step 1: Analyze scrape_configs job_name and targets

    The job 'cadvisor' targets 'localhost:8080', which is the default cAdvisor port.
  2. Step 2: Understand Prometheus scraping behavior

    Prometheus will scrape metrics from the specified target, not Docker Engine API or all containers directly.
  3. Final Answer:

    Scrape metrics from cAdvisor running on localhost port 8080 -> Option D
  4. Quick Check:

    Prometheus scrapes targets listed = Scrape metrics from cAdvisor running on localhost port 8080 [OK]
Hint: Targets in scrape_configs define where Prometheus scrapes [OK]
Common Mistakes:
  • Confusing cAdvisor with Docker Engine API
  • Assuming Prometheus scrapes containers directly
  • Thinking config syntax is invalid
4. You started Prometheus and cAdvisor with Docker Compose, but Prometheus shows no metrics from cAdvisor. What is the likely cause?
medium
A. Prometheus cannot scrape metrics from port 9090
B. Prometheus config has wrong job_name
C. cAdvisor container is not exposing port 8080 to Prometheus
D. Docker Compose file is missing image tags

Solution

  1. Step 1: Check port exposure between cAdvisor and Prometheus

    If cAdvisor does not expose port 8080 or network is misconfigured, Prometheus cannot scrape metrics.
  2. Step 2: Verify other options

    Wrong job_name or missing image tags usually cause errors but not silent no metrics; port 9090 is Prometheus default, not cAdvisor.
  3. Final Answer:

    cAdvisor container is not exposing port 8080 to Prometheus -> Option C
  4. Quick Check:

    Port exposure needed for scraping = cAdvisor container is not exposing port 8080 to Prometheus [OK]
Hint: Check port mapping between containers first [OK]
Common Mistakes:
  • Ignoring port exposure issues
  • Assuming job_name mismatch causes no metrics
  • Confusing Prometheus and cAdvisor ports
5. You want to monitor multiple Docker hosts with Prometheus and cAdvisor. Which setup is best?
hard
A. Run cAdvisor on each host exposing metrics, configure Prometheus to scrape all hosts
B. Run one cAdvisor container on a central host to monitor all hosts
C. Run Prometheus on each host and aggregate metrics manually
D. Use Docker Compose to run a single cAdvisor and Prometheus on one host only

Solution

  1. Step 1: Understand cAdvisor scope

    cAdvisor monitors containers on the host it runs on; to monitor multiple hosts, run cAdvisor on each host.
  2. Step 2: Configure Prometheus for multi-host scraping

    Prometheus can scrape metrics from multiple cAdvisor instances by listing all hosts in its config.
  3. Step 3: Evaluate other options

    Central cAdvisor cannot monitor other hosts; running Prometheus on each host complicates aggregation; single host setup misses other hosts.
  4. Final Answer:

    Run cAdvisor on each host exposing metrics, configure Prometheus to scrape all hosts -> Option A
  5. Quick Check:

    One cAdvisor per host + Prometheus scrape all = Run cAdvisor on each host exposing metrics, configure Prometheus to scrape all hosts [OK]
Hint: One cAdvisor per host, Prometheus scrapes all [OK]
Common Mistakes:
  • Thinking one cAdvisor can monitor all hosts
  • Running multiple Prometheus instances unnecessarily
  • Ignoring multi-host scraping config
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard