In containerized environments, monitoring plays a vital role in ensuring performance, reliability, and resource efficiency. Docker containers can host multiple microservices, each consuming CPU, memory, and I/O resources differently. To get complete visibility into your container metrics, cAdvisor (Container Advisor) and Prometheus make a perfect monitoring duo. Let us get through how to do this.

How to Monitor Docker Containers with cAdvisor and Prometheus

Note: Since setting up container monitoring requires root-level Docker access, this is an excellent project for a KVM VPS or a dedicated server environment, rather than a standard shared hosting plan.

What is cAdvisor?

cAdvisor (Container Advisor) is an open-source monitoring tool developed by Google to analyze and expose performance metrics for running containers. It runs as a daemon and collects information about CPU, memory, network, and disk usage in real time.

Key features of cAdvisor:

  • Collects per-container resource usage statistics.
  • Provides a built-in web UI for real-time monitoring.
  • Exposes metrics in a Prometheus-friendly format.
  • Lightweight and simple to deploy using Docker.

What is Prometheus?

Prometheus is a powerful open-source monitoring and alerting toolkit designed for time-series data. It scrapes metrics from configured targets (like cAdvisor) and stores them in its internal database. You can then query this data using PromQL or visualize it using Grafana.

Why use Prometheus with cAdvisor?

  • Prometheus automatically scrapes cAdvisor metrics at regular intervals.
  • You can set up alerts using Alertmanager.
  • Data can be visualized using Grafana dashboards.
  • Enables long-term storage and advanced querying of container metrics.

Prerequisites

Before starting, ensure you have:

  • Docker installed and running on your VPS.
  • Basic knowledge of Docker Compose (optional but recommended).
  • Ports 8080 and 9090 available on your system (for cAdvisor and Prometheus respectively).

Step 1: Deploy cAdvisor with Docker

cAdvisor can be run as a standalone container. Run the following command (assuming you are logged in as root):

docker run -d \
--name=cadvisor \
--volume=/:/rootfs:ro \
--volume=/var/run:/var/run:rw \
--volume=/sys:/sys:ro \
--volume=/var/lib/docker/:/var/lib/docker:ro \
--publish=8080:8080 \
--detach=true \
gcr.io/cadvisor/cadvisor:latest

Now, open your browser and visit: http://localhost:8080 or http://your_IP:8080. You will see a live dashboard displaying your running containers, CPU usage, memory stats, and more.

Step 2: Configure Prometheus to Scrape cAdvisor Metrics

Next, you need to set up Prometheus to collect metrics from cAdvisor. Create a prometheus.yml configuration file with the following content:

global:
  scrape_interval: 15s

scrape_configs:
  - job_name: 'cadvisor'
    static_configs:
      - targets: ['cadvisor:8080']

This tells Prometheus to scrape data from cAdvisor every 15 seconds.

Step 3: Run Prometheus in Docker

Run Prometheus using the following command:

docker run -d \
--name=prometheus \
-p 9090:9090 \
-v $(pwd)/prometheus.yml:/etc/prometheus/prometheus.yml \
prom/prometheus

Now visit: http://localhost:9090 or http://your_IP:9090. You should see the Prometheus web UI. Go to Status >> Targets to verify that cAdvisor is being scraped successfully.

Step 4: Using Docker Compose for Simplified Deployment

For easier management, you can run cAdvisor, Prometheus, and Grafana together using Docker Compose. Here is a minimal docker-compose.yml example:

version: '3'
services:
  cadvisor:
    image: gcr.io/cadvisor/cadvisor:latest
    ports:
      - "8080:8080"
    volumes:
      - /:/rootfs:ro
      - /var/run:/var/run:rw
      - /sys:/sys:ro
      - /var/lib/docker/:/var/lib/docker:ro
  prometheus:
    image: prom/prometheus
    ports:
      - "9090:9090"
    volumes:
      - ./prometheus.yml:/etc/prometheus/prometheus.yml

Then simply run:

docker-compose up -d

Useful PromQL Queries

Here are some example queries you can use in Prometheus or Grafana once data is flowing:

  • CPU usage per container: rate(container_cpu_usage_seconds_total[1m])
  • Memory usage: container_memory_usage_bytes
  • Disk I/O: rate(container_fs_reads_bytes_total[1m])
  • Network traffic: rate(container_network_transmit_bytes_total[1m])

Conclusion

By integrating cAdvisor with Prometheus, you can build a complete container monitoring system with minimal setup. Monitoring is not just about collecting data, it is about understanding your infrastructure. With cAdvisor and Prometheus, you gain that visibility, helping you keep your Docker workloads healthy and efficient.


📺 Watch the Video Tutorial on RackNerdTV

For a visual walkthrough on managing Docker containers visually on your server, check out our highly relevant video guide on installing Portainer on the RackNerdTV YouTube channel:

At RackNerd, you can deploy a Linux VPS with the flexibility and root-level access needed to host your own Docker monitoring stack and manage your environment seamlessly. If you prefer a hands-off approach for standard website hosting, you can also explore our shared web hosting plans.

👉 Visit https://racknerd.com to learn more about our hosting solutions.

Server Hosting Solutions by RackNerd:

Shared Hosting
cPanel Web Hosting in US, Europe, and Asia datacenters
Logo
Reseller Hosting
Create your new income stream today with a reseller account
Logo
VPS (Virtual Private Server)
Fast and Affordable VPS services - Instantly Deployed
Logo
Dedicated Servers
Bare-metal servers, ideal for the performance-demanding use case.
Logo

Leave a comment

Your email address will not be published. Required fields are marked *