Skip to content

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

Key points

  • Set memory and CPU limits, then measure usage against them.
  • Add HEALTHCHECKs so Docker knows when a container is broken.
  • Restart counts reveal crash loops that uptime checks may miss.
  • Ship logs off the host before the container is removed.

Quick checks

bash
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=oom

Health checks in Compose

compose.yamlyaml
services:
  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" }

Alerts worth setting

AlertWhy
Container unhealthy for 2 minutesIts own health check says it is broken
Restart count increasedCrash loop or OOM
OOMKilled trueMemory limit too low or a leak
Memory above 90% of limitAn OOM kill is coming
Host disk above 85%Images, volumes and logs fill disks
Public URL failingThe 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.

Restart policies and what they hide

PolicyBehaviourMonitoring note
noNever restartA crash means downtime; alert on exit
on-failure[:N]Restart on non-zero exit, up to N timesWatch the restart count
unless-stoppedAlways restart unless stopped by handCrash loops look like "Up 3 seconds"
alwaysAlways restart, also after daemon restartSame 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%.

Frequently asked questions

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.

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.