Skip to main content

Monitoring and Performance Tuning in Docker

Introduction

As you deploy more applications with Docker, ensuring that your containers run efficiently and reliably becomes crucial. Monitoring helps you gain visibility into resource usage, detect issues, and maintain high performance. Performance tuning enables you to optimize containers and the Docker host, preventing resource contention and bottlenecks.

In this lesson, you'll learn how to monitor Docker containers and hosts, use built-in and third-party tools, and apply performance tuning strategies. By the end, you'll be equipped to proactively manage your Dockerized environments for optimal performance.


Table of Contents

  1. Why Monitor Docker?
  2. Built-in Docker Monitoring Tools
  3. Exporting Metrics for Advanced Monitoring
  4. Performance Tuning Docker Containers
  5. Monitoring the Docker Host
  6. Common Monitoring and Tuning Pitfalls
  7. Summary
  8. Quiz

Why Monitor Docker?

  • Resource Management: Prevent containers from consuming excessive CPU, memory, or disk I/O.
  • Problem Detection: Identify failing containers, memory leaks, or runaway processes.
  • Performance Optimization: Tune containers and hosts for better throughput and lower latency.
  • Capacity Planning: Anticipate scaling needs and avoid resource exhaustion.

Built-in Docker Monitoring Tools

Docker provides several built-in commands to monitor containers and the Docker host.

docker stats

The docker stats command provides real-time metrics for running containers, including CPU, memory, network, and disk I/O.

Example:

docker stats

Sample Output:

CONTAINER ID   NAME         CPU %     MEM USAGE / LIMIT     NET I/O           BLOCK I/O
b4c7b6f7e8bc web_app 2.54% 128.5MiB / 512MiB 1.2MB / 2.1MB 300kB / 1.4MB

Use case: Quickly spot containers over-consuming resources.


docker top

Shows running processes inside a container, similar to the Linux top command.

Example:

docker top web_app

Use case: Debug high CPU/memory usage by inspecting running processes.


docker events

Streams real-time events from the Docker daemon (container start, stop, die, etc.).

Example:

docker events

Use case: Monitor lifecycle changes or automate responses to events.


Exporting Metrics for Advanced Monitoring

For production environments, you'll want to aggregate, visualize, and alert on Docker metrics using external tools.

Docker API and /metrics Endpoint

The Docker daemon exposes a REST API and, if enabled, a Prometheus-friendly /metrics endpoint.

  • Enable metrics in /etc/docker/daemon.json:
    {
    "metrics-addr": "0.0.0.0:9323",
    "experimental": true
    }
  • Restart Docker, then scrape metrics from http://localhost:9323/metrics.

Prometheus and cAdvisor

  • cAdvisor: Collects, aggregates, and exports container metrics.
  • Prometheus: Scrapes and stores metrics, supports alerting and visualization.

Run cAdvisor as a container:

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

Use case: Set up dashboards, create alerts for high memory/CPU usage, and analyze trends.


Performance Tuning Docker Containers

Fine-tuning resource allocations ensures fair sharing and prevents "noisy neighbor" issues.

Limiting CPU and Memory

CPU Limits

  • --cpus: Limits the number of CPUs accessible to a container.

    docker run --cpus=1.5 nginx
  • --cpu-shares: Relative weight vs. other containers (default: 1024).

    docker run --cpu-shares=512 nginx

Memory Limits

  • --memory or -m: Maximum memory allocation.

    docker run -m 512m nginx
  • --memory-swap: Maximum combined memory + swap.

    docker run -m 512m --memory-swap=1g nginx

Best Practice: Always set memory limits for production containers to avoid host OOM (Out of Memory) issues.


Block I/O Limits

Control container disk read/write rates to prevent disk contention.

docker run --device-read-bps /dev/sda:10mb --device-write-bps /dev/sda:5mb nginx

Tuning Networking

  • Network Modes: Bridge, host, overlay; choose based on use case.
  • Bandwidth Controls: Use Linux traffic control (tc) for advanced shaping.
  • DNS and Latency: Optimize container DNS settings to reduce lookup delays.

Monitoring the Docker Host

Container performance depends on the underlying host. Monitor:

  • CPU, Memory, Disk, Network: Use tools like top, htop, iotop, nload, or system monitoring suites.
  • Docker Daemon Logs: Check logs for warnings/errors (journalctl -u docker or /var/log/docker.log).
  • Resource Limits: Avoid oversubscribing host resources.

Common Monitoring and Tuning Pitfalls

  • No Resource Limits: Containers without limits can crash the host.
  • Ignoring Host Bottlenecks: Monitoring only containers and not the host.
  • Over-tuning: Setting limits too low can cause application failures.
  • Lack of Alerting: Not setting alerts for critical thresholds.
  • Not Monitoring Swarm/Kubernetes Layer: In orchestrated setups, monitor both containers and the orchestrator.

Summary

  • Docker provides built-in commands (stats, top, events) for basic monitoring.
  • Advanced monitoring uses Docker API, cAdvisor, and Prometheus.
  • Performance tuning involves setting CPU, memory, and I/O limits per container.
  • Always monitor both containers and the Docker host.
  • Avoid common pitfalls by setting limits, monitoring everything, and configuring alerts.

Quiz

1. Which command provides real-time CPU and memory usage for running Docker containers?

A) docker logs
B) docker top
C) docker stats
D) docker inspect

Details

Answer C) docker stats


2. What is the risk of running containers without memory limits?

A) Containers may not start
B) Containers can crash the host by exhausting memory
C) Containers will run faster
D) Docker will automatically limit them

Details

Answer B) Containers can crash the host by exhausting memory


3. Which tool is commonly used to collect and export container metrics for Prometheus?

A) Grafana
B) cAdvisor
C) Docker Hub
D) Docker Compose

Details

Answer B) cAdvisor


4. How do you limit a container to 1GB of memory and 2 CPUs? Give the Docker command.

Answer
docker run --memory=1g --cpus=2 <image>

5. True or False: Monitoring only containers is sufficient for production environments.

Details

Answer False — You must monitor both containers and the Docker host.