Skip to content

Lesson 18 of 19 · 18m

Logs Fill Disks, Healthchecks Don't Restart Anything, and Restart Policies Have a Grammar

json-file logs grow unbounded by default, HEALTHCHECK reports status to nothing, and the four restart policies differ in exactly when they fire.

The default logging driver, json-file, writes every byte of stdout and stderr to /var/lib/docker/containers/<id>/<id>-json.log. Forever. There is no default rotation. The first time this matters is the night a chatty container fills the disk and takes the daemon — and everything else on the host — down with it.

json-file never rotates itself

Configure it before production learns it for you. Globally, in /etc/docker/daemon.json:

{
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "10m",
    "max-file": "3"
  }
}

Per-container the same keys go in --log-opt or Compose's logging: block. Three 10 MB files is a 30 MB ceiling per container — bounded, boring, correct. The local driver does the same job with less overhead (~100 MB, block-structured). Pick journald if you already run systemd and want journalctl CONTAINER_NAME=web as your interface.

Know the retrieval limit: docker logs works only for json-file, local, and journald. Ship to fluentd, gelf, or syslog and logs leave the host — docker logs then answers "configured logging driver does not support reading," and your logs live in whatever you pointed at. Which is fine — that is what a real pipeline is — as long as nobody's runbook says docker logs first.

HEALTHCHECK reports. Nothing acts on the report.

HEALTHCHECK CMD curl -fs http://localhost/ || exit 1 — with --interval, --timeout, --start-period, --retries — makes docker ps print (healthy) or (unhealthy) and fills docker inspect -f '{{json .State.Health}}' with a log of recent check output. That is all it does. Plain Docker will not restart, replace, or remove an unhealthy container; health is metadata. Something has to consume it — Swarm's health-aware scheduling, autoheal-style watchers, your own supervisor — or the flag is decoration. Compose's depends_on: {db: {condition: service_healthy}} gates startup order only; it does nothing after the services are up.

Restart policies have a grammar

--restart takes four values with real semantic differences:

  • no — never restart. Default.
  • on-failure[:N] — restart only on non-zero exit, at most N times. Zero exit means deliberate stop; Docker respects it.
  • always — restart on any exit and after daemon restarts — including containers you stopped by hand, which surprises people after the daemon reboots.
  • unless-stopped — restart on crash and daemon restart, but honor a manual docker stop. The sane default for daemons you babysit.

docker update --restart unless-stopped web retrofits it without recreating. Exit code 137 means the kernel killed the process — check docker inspect for State.OOMKilled: true before blaming your app; that is the cgroup memory limit doing its job, the same limit the read-only-rootfs lesson set.

The events stream is the timeline

docker events --since 2h prints the lifecycle feed — create, start, die, kill, oom, health_status: unhealthy — as it happened. When a container disappears at odd hours, this is where the answer lives, alongside docker logs on the dead container (logs survive the exit; the writable layer survives too, until docker rm or prune eats it). docker inspect then gives State.ExitCode, FinishedAt, OOMKilled, Error — the postmortem in one object.

Try this

Run docker run -d --name noisy alpine sh -c 'while :; do echo x; done' and watch ls -lh /var/lib/docker/containers/*/. Now imagine a year of it. Kill it, then docker logs noisy — the evidence outlived the process.

Field notes

  • docker logs --since, --tail, -f compose; --details adds the log-opt metadata.
  • docker compose up -d recreates containers when the image digest or config changes — "the container restarted" is often "the container was replaced," and docker events shows which.
  • stop_grace_period in Compose mirrors docker stop -t — default 10 seconds between SIGTERM and SIGKILL, same rule the PID 1 lesson covers.
  • A container with no HEALTHCHECK defined has no State.Health at all — docker inspect omits the field entirely.
  • fluentd/gelf/syslog drivers block container start when the endpoint is down unless mode: non-blocking is set — a logging outage can take your deploy with it.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.