How-to guide
Docker container monitoring
On a Docker host or a Compose stack, containers restart quietly and logs vanish with them. Watch resources against limits, health status, restarts and logs.
3 min read11 sectionsUpdated
On this page
docker stats --no-stream --format 'table {{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}\t{{.MemPerc}}'
docker ps --format 'table {{.Names}}\t{{.Status}}' # (healthy), (unhealthy), Restarting
docker inspect -f '{{.Name}} restarts={{.RestartCount}} oom={{.State.OOMKilled}}' $(docker ps -aq)
docker logs --since 10m --tail 200 <container>
docker events --since 30m --filter event=die --filter event=oomservices:
api:
image: registry.example.com/api:2.3.1
restart: unless-stopped
deploy:
resources:
limits: { cpus: "1.0", memory: 512M }
healthcheck:
test: ["CMD", "wget", "-qO-", "http://localhost:8080/health/live"]
interval: 15s
timeout: 3s
retries: 3
start_period: 30s
logging:
driver: json-file
options: { max-size: "10m", max-file: "5" }| Alert | Why |
|---|---|
| Container unhealthy for 2 minutes | Its own health check says it is broken |
| Restart count increased | Crash loop or OOM |
| OOMKilled true | Memory limit too low or a leak |
| Memory above 90% of limit | An OOM kill is coming |
| Host disk above 85% | Images, volumes and logs fill disks |
| Public URL failing | The whole stack, seen as a user sees it |
Logs
Container logs live on the host and disappear when containers are removed. Rotate them (as above) and ship them centrally: a log driver, a sidecar such as Fluent Bit, or an OpenTelemetry Collector reading container log files. See log aggregation.
In VitalRix
The VitalRix agent, or an OpenTelemetry Collector, reports host and container metrics to infrastructure monitoring, shown on a map coloured by CPU, memory or disk, where a host that stopped reporting appears as a dashed grey tile. Uptime checks watch the services from outside, and APM traces the applications inside the containers. VitalRix itself can also be installed on your own servers with Docker Compose.
| Policy | Behaviour | Monitoring note |
|---|---|---|
| no | Never restart | A crash means downtime; alert on exit |
| on-failure[:N] | Restart on non-zero exit, up to N times | Watch the restart count |
| unless-stopped | Always restart unless stopped by hand | Crash loops look like "Up 3 seconds" |
| always | Always restart, also after daemon restart | Same as above; alert on restarts |
Disk: the quiet killer on Docker hosts
Images, stopped containers, build cache, volumes and unrotated logs fill disks slowly, and a full disk stops every container on the host at once. Check with docker system df, prune old images on a schedule (docker image prune -a --filter "until=168h"), rotate logs as shown above, and alert on host disk above 85%.
Is docker stats enough for monitoring?
It is fine for a quick look. For history, alerts and many hosts, send the same metrics to a monitoring backend.
Do HEALTHCHECKs restart containers?
Plain Docker marks the container unhealthy but does not restart it on its own. Orchestrators and some setups act on it; otherwise alert on the status.
Related reading
- Exit code 137: what it means in Docker and Kubernetes: Exit code 137 means a container received SIGKILL. Learn to tell memory limits from slow shutdowns and manual kills, with Docker and Kubernetes commands.
- Docker "port is already allocated": how to fix it: Fix Docker "Bind for 0.0.0.0 failed: port is already allocated": find the process or container on the port, clear stale containers and change mappings.
- Server and Kubernetes monitoring: See CPU, memory, disk and running instances for servers, Docker containers and Kubernetes pods beside uptime checks, logs and traces, with a one-file agent.
- Error: connect ECONNREFUSED: What connect ECONNREFUSED means and why it happens with databases, Redis and APIs, especially in Docker where localhost is the container.
- HAProxy monitoring: backends, errors and queues: Monitor HAProxy: the stats page and Prometheus endpoint, backend server health, 5xx rates, queue length, session limits and response times, with alerts to set.
- Docker "no space left on device": how to free disk space: Fix Docker no space left on device: find what fills the disk, prune images and build cache safely, cap container logs and avoid deleting volumes by mistake.
Watch your own site and services with VitalRix
Uptime checks, APM, logs, real-user monitoring, alerts and status pages in one place. Every product has a free tier and no credit card is needed.