Container Monitoring

Monitoring containers differs from monitoring traditional servers in one key way: containers are ephemeral. A container might be recreated with a new ID at any time, making container-by-ID monitoring unreliable. Modern container monitoring uses labels and service names to track metrics across container restarts. The monitoring approach scales from simple (docker stats) to production-grade (Prometheus + cAdvisor + Grafana).

Docker built-in monitoring

# Live resource usage for all containers:
docker stats

docker stats output

CONTAINER ID   NAME    CPU %    MEM USAGE / LIMIT    MEM %   NET I/O         BLOCK I/O
abc123         web     0.52%    45.2MiB / 512MiB     8.83%   2.3GB / 456MB   12MB / 890MB
def456         db      1.24%    256.8MiB / 1GiB      25.1%   123MB / 67MB    890MB / 4.5GB
# Non-interactive (for scripting):
docker stats --no-stream --format "table {{.Name}}	{{.CPUPerc}}	{{.MemUsage}}"

# Inspect a specific container's resource usage:
docker inspect mycontainer | grep -A5 "Memory\|CpuShares"

Prometheus for containers

# cAdvisor exposes container metrics to Prometheus:
docker run -d   --name cadvisor   --privileged   -p 8080:8080   -v /:/rootfs:ro   -v /var/run:/var/run:ro   -v /sys:/sys:ro   -v /var/lib/docker/:/var/lib/docker:ro   gcr.io/cadvisor/cadvisor:latest

# Add cAdvisor to Prometheus scrape config:
# scrape_configs:
#   - job_name: 'cadvisor'
#     static_configs:
#       - targets: ['localhost:8080']

# Key metrics available:
# container_cpu_usage_seconds_total  — CPU usage per container
# container_memory_usage_bytes       — memory per container
# container_network_transmit_bytes_total — network per container

Container log management

# View container logs:
docker logs myapp                     # All logs
docker logs myapp --tail 100          # Last 100 lines
docker logs myapp -f                  # Follow (like tail -f)
docker logs myapp --since 1h          # Last hour
docker logs myapp --since "2025-06-09T14:00:00"

# Prevent log files from filling disk (set in /etc/docker/daemon.json):
# {
#   "log-driver": "json-file",
#   "log-opts": { "max-size": "100m", "max-file": "3" }
# }

# Ship container logs to syslog/Fluentd/ELK:
docker run --log-driver=syslog --log-opt syslog-address=tcp://logserver:514 myapp

Conclusion

For production container monitoring, the standard stack is cAdvisor (container metrics exporter) + Prometheus (metrics storage) + Grafana (dashboards). Import the Docker dashboard from Grafana's community library (ID 893 or 14282) to immediately get CPU, memory, network, and restart counts per container. Always configure log rotation in /etc/docker/daemon.json — a verbose application running for months without log rotation fills the disk and crashes the Docker host.

FAQ

Is Container Monitoring important for Ubuntu administrators?+

Yes. It supports practical Ubuntu administration because it connects directly to server reliability, security, troubleshooting, or daily operations.

Should I practice this on a live server?+

Use a lab VM first. After you understand the command output and rollback path, apply the workflow carefully on real systems.

What should I do after reading this article?+

Run the practice commands, write down what each one shows, and continue to the next article in the Ubuntu roadmap.

Need help with Ubuntu administration?

Work directly with Muhammad Irfan Aslam for Ubuntu Server, Linux, cloud, Docker, DevOps, CI/CD, or infrastructure troubleshooting support.

Hire Me for Support