Marcio Cunha

How to monitor Docker containers with Prometheus and Grafana

Learn how to configure Docker exporters to monitor CPU, memory, and disk I/O in production containerized environments using Prometheus and Grafana.

Marcio Cunha•4 min
Also available in:PortuguêsEspañol
Summary
  • Exporters translate internal operating system and container metrics into formats that Prometheus understands seamlessly.
  • cAdvisor collects granular resource consumption data directly from the Linux kernel for each active Docker container.
  • Grafana dashboards transform raw time-series data into intuitive charts for effective production decision-making.
  • Properly tuning resource limits in Docker prevents a single container from compromising the stability of the entire infrastructure.
  • Proactive alerts based on CPU and I/O metrics stop outages before they ever impact end users.

Why monitor containers in production environments?

Managing containerized applications without an observability tool is like driving a car at night without a dashboard. In the modern ecosystem, where services spin up and down in seconds, knowing exactly how much hardware each application consumes is no longer optional. In practice, this means that long before a system crashes due to a memory shortage, you have already received an alert on your phone and can take action. To understand the impact of this on your infrastructure, it is also worth checking our guide on how to tune Prometheus rules to avoid unnecessary noise for your operations team.

When we run dozens of isolated services in Docker, the native visibility of the stats command becomes insufficient for historical diagnostics. We need an architecture that collects, stores, and displays trends over time. This is where two of the most powerful tools in open-source technology come in: Prometheus, which acts as an untiring collector of numeric data, and Grafana, responsible for rendering those numbers into colorful, easy-to-read panels.

Understanding the role of cAdvisor in metric collection

Prometheus on its own does not know what a Docker container is; it simply reads web pages formatted with numerical metrics. This is where cAdvisor comes in, a helper program created by Google that runs alongside your services gathering CPU, memory, network, and disk usage data for each container. In practice, it acts as a translator that looks at the Docker engine and tells Prometheus exactly what is happening under the hood. If you are expanding your infrastructure to alternative hardware, it is worth checking how to run Docker containers on RISC-V in your homelab to test new hardware horizons.

Setting up cAdvisor is straightforward and usually requires just running a dedicated container with access to the Docker sockets on the host. This component consumes very few resources and delivers impressive detail, allowing you to audit everything from the average consumption of a web application to deep read and write bottlenecks on disk.

Step-by-step guide to configure Prometheus and cAdvisor

To get this engine running in your production environment, we will use Docker Compose, a tool that helps spin up multiple connected containers in an organized way. Create a structured configuration file and ensure communication ports are properly isolated or protected by secure internal networks.

  1. Create a file named docker-compose.yml in your working folder and paste the base structure below to spin up cAdvisor and Prometheus.
  2. Insert the functional cAdvisor code block pointing to essential Linux host directories for correct kernel data reading.
    version: '3.8'
    services:
      cadvisor:
        image: gcr.io/cadvisor/cadvisor:latest
        container_name: cadvisor
        ports:
          - '8080:8080'
        volumes:
          - /:/rootfs:ro
          - /var/run:/var/run:ro
          - /sys:/sys:ro
          - /var/lib/docker/:/var/lib/docker:ro
        restart: unless-stopped
  3. Add the Prometheus service in the same YAML file, configuring the rules file to point to the newly created cAdvisor address.

After saving the file, execute the startup command in your terminal to bring the services into continuous operation. It is crucial to ensure that the Prometheus configuration file (prometheus.yml) correctly points to the cAdvisor target on port 8080, allowing periodic scraping of performance data.

Visualizing CPU and memory data in Grafana

With Prometheus collecting metrics every fifteen seconds, the next step is connecting Grafana to this database to start drawing charts. Grafana connects to Prometheus as a native data source, requiring only the insertion of the internal Prometheus server address. From there, you can import ready-made community dashboards or create custom panels focused strictly on the health of your cluster.

On CPU panels, for example, you will be able to see if the core limits configured in Docker are throttling any application under heavy load. In the memory tab, the focus should be monitoring real RAM usage versus swap memory, identifying code leaks before they cause the operating system to forcefully terminate the process. Maintaining this monitoring rigor prevents unpleasant surprises during peak traffic hours.

Interpreting I/O bottlenecks in container disks

Disk usage is often the Achilles' heel of containerized production applications, especially databases or systems handling many temporary files. cAdvisor exposes detailed metrics of bytes read and written, alongside the latency of block I/O operations. In practice, this means that if an application starts lagging due to storage slowdowns, you will know precisely which container is generating the most write load.

When we identify persistent slowdown issues linked to physical hardware or the underlying storage subsystem, it is important to remember that the bottleneck might originate from the physical server itself. For scenarios where thermal stress or component wear affects overall machine performance, it is worth consulting the guidelines on how to fix laptop thermal throttling and restore peak performance on supporting workstations.

Final considerations on container observability

Implementing a solid monitoring stack with Prometheus and Grafana completely transforms the daily routine of any engineering and operations team. Instead of fighting fires in the dark and guessing the cause of an outage, you gain access to precise data, consistent history, and intelligent alerts. The discipline of observing container behavior in real time ensures stability, reduces operating costs, and elevates company-wide trust in the services delivered to customers.