restart: unless-stopped hides a crash loop
Docker restarts a crashing container in a tenth of a second, and docker ps says Up. Here is where Docker records the crashes, and what erases the record.
You put this on every service, because a service that crashes at three in the morning should come back on its own:
services:
api:
image: shop/api
restart: unless-stopped
It does come back. Docker starts it again about a tenth of a second after it dies, and docker ps reports Up 5 seconds. Nothing turns red, and nobody gets a message. The restart policy did its job.
The service can crash every twelve seconds for a week, and that view does not change. Docker records every crash, but not where you look. And the first thing most people do when they finally notice, docker restart, erases the record.
The commands and the output below come from Docker Engine 29.1 and Compose 5.1. You can repeat them with the alpine image in a few minutes.
What a restart policy does
A restart policy acts when the main process of a container exits. It does nothing for a container that hangs, or for one that Docker marks unhealthy. Both keep running.
| Policy | Process exits with an error | Process exits with 0 | You run docker stop |
|---|---|---|---|
no (the default) |
stays stopped | stays stopped | stays stopped |
on-failure:3 |
restarts, at most 3 times | stays stopped | stays stopped |
always |
restarts | restarts | stays stopped until the daemon restarts |
unless-stopped |
restarts | restarts | stays stopped |
Note the middle column. always and unless-stopped also restart a process that finished cleanly, so a one-shot job such as a database migration runs again and again.
The backoff, and the 10 seconds that reset it
Docker waits before each restart. The first wait is 100 milliseconds, and every restart doubles it, up to one minute. When a run lasts 10 seconds or more, the wait drops back to 100 milliseconds. Those are three constants in the restart manager of the daemon.
That reset decides what you see. Start two containers. One crashes after one second, the other after twelve:
$ docker run -d --name fast --restart unless-stopped alpine sh -c 'sleep 1; exit 1'
$ docker run -d --name slow --restart unless-stopped alpine sh -c 'sleep 12; exit 1'
Less than two minutes later:
$ docker ps --format 'table {{.Names}}\t{{.Status}}'
NAMES STATUS
slow Up 5 seconds
fast Restarting (1) 41 seconds ago
fast never runs for 10 seconds, so its wait doubles until it reaches the minute, and docker ps shows Restarting most of the time. That loop is loud. slow runs for 12 seconds every time, so its wait never grows. Docker starts it again 100 milliseconds after each crash, and at almost any moment you look, docker ps shows Up. Real services crash like slow more often than like fast: a lost database connection, one bad request, a memory leak that reaches the limit after an hour.
The Docker documentation says that a restart policy takes effect only after a container has been up for at least 10 seconds, to prevent a restart loop. The daemon does not do that. In the source, the 10 seconds only reset the wait, and in the test above fast restarted ten times in under two minutes without ever staying up for two seconds.
Compose shows the same picture. Its only hint is the gap between two columns:
$ docker compose ps --format 'table {{.Service}}\t{{.RunningFor}}\t{{.Status}}'
SERVICE CREATED STATUS
api 7 minutes ago Up 2 seconds
db 8 minutes ago Up 8 minutes
api was created seven minutes ago and has been up for two seconds. It crashed 35 times in between.
Where Docker keeps the evidence
The restart count. Docker counts every restart that the policy makes and stores the count on the container. docker ps cannot show it, because the container list has no such field, so --format '{{.RestartCount}}' fails with a template error. Ask docker inspect:
$ docker inspect --format '{{.RestartCount}} {{.Name}}' $(docker ps -q) | sort -rn
10 /fast
8 /slow
Any number above zero on a service that nobody restarted on purpose is a crash.
The last exit. This one misleads. docker inspect has State.ExitCode, but Docker sets it back to 0 each time the container starts again:
$ docker inspect --format '{{.State.Status}} exit={{.State.ExitCode}} started={{.State.StartedAt}} finished={{.State.FinishedAt}}' slow
running exit=0 started=2026-11-17T15:33:30.804465224Z finished=2026-11-17T15:33:30.781535974Z
slow has never exited with 0. While it runs, the only trace of the last crash is FinishedAt, 23 milliseconds before StartedAt. The real exit code shows only in the instant between two runs.
The events. The daemon emits a die event for every exit, with the exit code and the length of the run in seconds:
$ docker events --filter event=die \
--format '{{.Actor.Attributes.name}} exited {{.Actor.Attributes.exitCode}} after {{.Actor.Attributes.execDuration}}s'
fast exited 1 after 1s
fast exited 1 after 1s
slow exited 1 after 12s
fast exited 1 after 1s
slow exited 1 after 12s
When the kernel kills a process for memory, an oom event comes just before the die. But docker events is a live stream. With --since it reaches back only through the last 256 events that the daemon keeps in memory, for all containers together, and a daemon restart empties that buffer.
The logs. docker logs keeps the output of every run of the same container, so the error before each crash sits between the startup lines. docker logs -f is less useful than it looks: it ends when the container dies, and it does not follow the next run.
Three exit codes cover most crashes:
1, or another small number, comes from your program. Its last log lines say why.137is 128 + 9, SIGKILL. After anoomevent, the kernel killed the process for memory. Without one, something else sent the kill.docker stopsends it when a process ignores SIGTERM for 10 seconds.143is 128 + 15, SIGTERM. Something asked the process to stop, and it did.
What erases it
| You run | Restart count | docker logs |
|---|---|---|
docker compose up -d with no change |
kept | kept |
docker restart or docker compose restart |
back to 0 | kept |
docker stop, then docker start |
back to 0 | kept |
docker compose up -d after a change to the service |
0, it is a new container | gone |
docker compose down |
the container is removed | gone |
The restart is the usual first reaction to a misbehaving service, and it is also the step that resets the count. So the order matters: read the count, the events, and docker logs first, then restart or redeploy.
What to do instead
Keep the restart policy. It turns a rare crash into a short gap: the restart wait plus the startup time of your service. Without one, the first crash is an outage that lasts until somebody wakes up. Change two things around it:
- Use
on-failurefor jobs that should end. A migration underunless-stoppedruns again after it succeeds. Underon-failure:3, a failed run is retried three times, and a successful one stays finished. - Watch the count, not the status. For a slow loop, the status reads
Upalmost all the time. The count only grows, until somebody restarts the container. A check that fires when the count went up in the last few minutes catches the fast loop and the slow one.
On a single host, the die events are enough to build that check. This loop posts every exit to a Slack incoming webhook:
docker events --filter event=die \
--format '{{.Actor.Attributes.name}} exited {{.Actor.Attributes.exitCode}} after {{.Actor.Attributes.execDuration}}s' |
while read -r line; do
curl -fsS -X POST -H 'Content-Type: application/json' \
--data "{\"text\": \"$line\"}" "$SLACK_WEBHOOK_URL"
done
Know its limits. It also reports every docker stop and every deploy, because those are exits too. It sees only what happens while it runs, so run it under systemd. And when the whole host goes down, it goes down with it, and nothing tells you.
How Lumin handles it
Lumin monitors Docker Compose stacks with one agent on your host. Every 15 seconds, the agent inspects each running container, including the ones that Docker is restarting at that moment. It sends the restart count as the metric container.restart_count, together with container.up, which is 0 while a container is restarting. Inspect is one of the five Docker requests that the socket proxy of the agent allows, as described in the article about docker.sock. The Infrastructure page shows the restarts and the uptime of every container.
The alert rule for the api service above is one line:
increase(container.restart_count) {container=shop-api-1} > 0 over 15m
increase adds up only the rises in the count. So a docker restart that sets the count back to 0 neither fires the rule nor hides the next crash. Leave out the filter, and one rule watches every container on every host. When the rule fires, Lumin posts the condition and the value to the Slack channel you connected, and it posts again after 15 minutes without a restart. Alert rules are part of the paid tiers. The agent documentation lists the other container rules.
The agent also ships the logs of containers that carry the lumin.logs=true label, and here a crash loop costs something. Docker ends a log stream when the container dies, as it does for docker logs -f, and the agent opens a new stream on its next sweep, within 30 seconds. A loop faster than that can lose whole runs. Trust the count, and read docker logs on the host for the complete output.
If you run a small stack and want uptime checks, logs, metrics, and Slack alerts without an SDK in your services, Lumin is live in about ten minutes.