Lesson 9 of 9 · 20m
Operating Containers in Production
Healthchecks, restart policy, logging, resource limits, and the day-two discipline that separates a running container from an operated one.
Getting a container running is the tutorial. Operating it — surviving deploys, crashes, disk pressure, and 3am pages — is the job. Most production container incidents trace to a handful of missing operational basics, and none of them are exotic.
Healthchecks: tell Docker what "alive" means
Without a healthcheck, Docker knows the process exists — nothing more. A deadlocked app is a healthy container. Declare what health means:
HEALTHCHECK --interval=30s --timeout=3s --start-period=15s --retries=3 CMD curl -fsS http://localhost:8080/healthz || exit 1--start-period matters more than people expect: slow-starting apps (JVMs, big Django apps) fail healthchecks during warmup and enter restart loops. The healthcheck endpoint itself should check dependencies that the app needs — but carefully: a healthcheck that fails when the database blips can cascade a momentary database hiccup into a full fleet restart.
Restart policy is a decision, not a default
no – dev only
always – restarts even after daemon restarts; right for single-host services
unless-stopped – same, but respects a manual docker stop across reboots
on-failure:n – only crash loops; the right choice for batch jobsThe trap: always on a container with a fatal config error produces a crash loop that hammers the database every few seconds forever. Pair restarts with healthchecks and alerting on restart count, or you'll find out from the downstream system.
Logging: the disk-full incident generator
Default json-file logging is unbounded. A chatty container will fill the host disk — every time, eventually. Configure rotation globally and per-container:
// /etc/docker/daemon.json
{
"log-driver": "json-file",
"log-opts": { "max-size": "50m", "max-file": "3" }
}docker run --log-opt max-size=10m --log-opt max-file=2 myappAnd log to stdout/stderr from the app, always — never to files inside the container. Files inside the container layer grow the overlay upperdir (unrotated!), disappear with the container, and can't be collected by any standard tooling. Twelve-factor wasn't writing poetry; stdout logging is how every log pipeline expects to receive.
Resource limits are seatbelts
A container without --memory can consume the host. One leaking service takes down everything sharing the node, and the kernel OOM killer picks victims with no respect for your priorities. Set memory and CPU on everything; set --pids-limit to defang fork bombs; monitor docker stats in anger at least once before trusting your numbers.
The upgrade drill
docker compose pull && docker compose up -d
# watch it: docker compose ps — healthy? then:
docker image prune -f # reclaim old layers, but know what you're deletingBlue-green on a single host is just: run the new container on a new port, healthcheck it, flip the reverse proxy, stop the old one. The pattern scales down as well as up — the point is that "restart the container and pray" is not a deploy strategy.
Field notes
docker compose up -ddoes not recreate containers whose config is unchanged — after editing compose files, checkdocker compose psfor actual recreation.- Pin the timezone or accept UTC. Mixed-timezone logs across a fleet turn every incident into timestamp arithmetic.
- Keep the host's disk under 70%: overlay filesystems degrade badly under pressure, and image pulls fail in ways that look like network errors.
- Alert on container restart counts and healthcheck status, not just host metrics. The container knows it's sick before the host does.
Was this lesson helpful?