Bird
Raised Fist0
Dockerdevops~10 mins

Prometheus for Docker monitoring - Step-by-Step Execution

Choose your learning style10 modes available
Process Flow - Prometheus for Docker monitoring
Start Docker Containers
↓
Run Prometheus Container
↓
Configure Prometheus to scrape Docker metrics
↓
Prometheus collects metrics
↓
Metrics stored in Prometheus database
↓
User queries metrics via Prometheus UI or API
↓
Visualize or alert based on Docker metrics
↓
End
This flow shows how Prometheus runs alongside Docker containers, collects metrics from them, stores data, and allows users to query or visualize Docker performance.
Execution Sample
Docker
version: '3'
services:
  prometheus:
    image: prom/prometheus
    volumes:
      - ./prometheus.yml:/etc/prometheus/prometheus.yml
    ports:
      - "9090:9090"
This Docker Compose snippet runs Prometheus with a custom config file and exposes its web UI on port 9090.
Process Table
StepActionResultNotes
1Start Docker containers including app and PrometheusContainers runningDocker daemon manages container lifecycle
2Prometheus container reads prometheus.yml configConfig loadedConfig includes Docker metrics scrape targets
3Prometheus scrapes Docker metrics endpointMetrics collectedMetrics include CPU, memory, network stats
4Metrics stored in Prometheus time-series databaseData storedData ready for queries
5User accesses Prometheus UI at localhost:9090UI loadsUser can query and visualize metrics
6User runs query for Docker container CPU usageQuery result shownReal-time CPU usage displayed
7User stops Prometheus containerPrometheus stopsMetrics collection ends
💡 Prometheus container stopped, monitoring ends
Status Tracker
VariableStartAfter Step 3After Step 4After Step 6Final
Prometheus StatusNot runningRunningRunningRunningStopped
Metrics DataNonePartial metrics collectedStored in DBQueried and displayedStored but no new data
User Query ResultNoneNoneNoneCPU usage data shownLast query result
Key Moments - 3 Insights
Why does Prometheus need a config file to monitor Docker?
Prometheus uses the config file (prometheus.yml) to know which Docker endpoints to scrape for metrics, as shown in step 2 of the execution table.
What happens if the Prometheus container stops?
Metrics collection stops immediately, but stored data remains until Prometheus restarts, as shown in step 7.
How does Prometheus get metrics from Docker containers?
Prometheus scrapes exposed metrics endpoints from Docker containers or Docker daemon, collecting data like CPU and memory usage (step 3).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step does Prometheus start collecting Docker metrics?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Check the 'Action' column for when metrics are scraped.
According to the variable tracker, what is the status of Prometheus after step 6?
ARunning
BNot running
CStopped
DStarting
💡 Hint
Look at the 'Prometheus Status' row under 'After Step 6'.
If the prometheus.yml config is missing the Docker scrape target, what changes in the execution table?
AStep 5 UI would not load
BStep 3 would not collect metrics
CStep 7 would not stop Prometheus
DStep 4 would store more data
💡 Hint
Refer to step 3 where metrics collection depends on config.
Concept Snapshot
Prometheus monitors Docker by running as a container alongside Docker apps.
It uses a config file to know which Docker endpoints to scrape.
Prometheus collects and stores metrics like CPU and memory usage.
Users query metrics via Prometheus UI on port 9090.
Stopping Prometheus stops metric collection but keeps stored data.
Full Transcript
This visual execution shows how Prometheus monitors Docker containers. First, Docker containers including Prometheus start. Prometheus reads its configuration file to find Docker metrics endpoints. Then it scrapes these endpoints to collect metrics such as CPU and memory usage. The collected data is stored in Prometheus's time-series database. Users can access the Prometheus web UI on port 9090 to query and visualize these metrics. When Prometheus stops, metric collection ends but stored data remains. Variables like Prometheus status and metrics data change step-by-step as shown in the tables.

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