📖 Scenario: You want to monitor your Docker containers' resource usage using Prometheus. Prometheus will collect metrics from Docker containers so you can see how much CPU and memory they use.
🎯 Goal: Set up a basic Prometheus configuration to monitor Docker containers by scraping metrics from the Docker daemon's metrics endpoint.
📋 What You'll Learn
Create a Docker Compose file with Prometheus service
Add Prometheus configuration file to scrape Docker metrics
Run Prometheus container with the configuration
Verify Prometheus is scraping Docker metrics
💡 Why This Matters
🌍 Real World
Monitoring Docker containers helps keep applications healthy and performant by tracking resource usage.
💼 Career
DevOps engineers often set up Prometheus to monitor containerized applications in production environments.
Progress0 / 4 steps
1
Create Docker Compose file with Prometheus service
Create a file named docker-compose.yml with a service called prometheus using the image prom/prometheus:latest. Map port 9090 on the host to port 9090 in the container. Mount the local file prometheus.yml to /etc/prometheus/prometheus.yml inside the container as a read-only volume.
Docker
Hint
Use the prom/prometheus:latest image and map port 9090. Mount the config file prometheus.yml as read-only.
2
Create Prometheus configuration file to scrape Docker metrics
Create a file named prometheus.yml with a scrape_configs section. Add a job named docker that scrapes metrics from http://host.docker.internal:9323/metrics every 15 seconds.
Docker
Hint
Use scrape_configs with a job named docker. Set scrape_interval to 15 seconds and target host.docker.internal:9323.
3
Run Prometheus container with Docker Compose
Run the command docker-compose up -d in the terminal to start the Prometheus container with the configuration.
Docker
Hint
Use docker-compose up -d to start Prometheus in detached mode.
4
Verify Prometheus is scraping Docker metrics
Run the command curl http://localhost:9090/metrics in the terminal and check that the output contains the string container_cpu_usage_seconds_total.
Docker
Hint
Use curl http://localhost:9090/metrics and look for container_cpu_usage_seconds_total in the output.
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
Step 1: Understand Prometheus purpose
Prometheus is designed to collect and store metrics data from various sources, including Docker containers.
Step 2: Differentiate from other Docker tools
Prometheus does not run containers, build images, or replace Docker Engine; it focuses on monitoring.
Final Answer:
To collect and store metrics from Docker containers -> Option B
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
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.
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.
Final Answer:
docker run --name cadvisor -p 8080:8080 gcr.io/cadvisor/cadvisor:latest -> Option A
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: