A Compose service does not come back after it crashes or the server reboots
Exited (0) in the STATUS column of docker ps -aShort answer
Compose restarts a container only if the service has a restart: value, and each value has limits. The default is no. on-failure ignores a clean exit and does not survive a daemon restart. Nothing restarts a container that you stopped by hand or that dies in under ten seconds.
Fix it fast
1Find out which cause you have
Ask Docker for the container state and how many restarts it has done. Together with the restart line in the Compose file, this tells you which cause below applies.
grep -n "restart" compose.yaml docker ps -a docker container inspect --format '{{.State.Status}} restarts={{.RestartCount}}' my-app2Match what you see, then jump to its fix
- 1
grep finds no restart: line for the service, and the container stays Exited after a crashThe service has no restart policyFix - 2
Exited (0) in docker ps -a, with restart: on-failure in the fileon-failure does not restart a container that exited with code 0Fix - 3
The container shows Exited seconds after start and RestartCount stays 0The container crashes before it has run for ten secondsFix - 4
You ran docker stop or docker compose stop before the reboot, and the container stayed down afterwardsThe container was stopped by handFix
- 1
What does the error look like?
Exited (0) in the STATUS column of docker ps -aExited (137) in the STATUS column of docker ps -a
Cause 1 of 4
The service has no restart policy
1Check
The default policy is no. Compose then does not restart the container under any circumstances.
grep -n "restart" compose.yaml2Fix
Set a policy on the service. unless-stopped is the usual choice for servers, on-failure:5 gives up after five tries.
services:
api:
image: my-app:latest
restart: unless-stopped3Verify
Recreate the container so the new policy applies, then confirm it is running.
docker compose up -d
docker compose psConfirmed by docs.docker.com
Cause 2 of 4
on-failure does not restart a container that exited with code 0
1Check
on-failure restarts only after a non-zero exit code. A program that ends normally, for example a script that finishes or a server that handles a shutdown signal and returns 0, is left stopped. on-failure also does not restart containers when the daemon restarts.
docker ps -a --filter name=my-app2Fix
If the service must always run, use unless-stopped or always. Keep on-failure for jobs that should be retried only when they fail.
services:
worker:
image: my-worker:latest
restart: unless-stopped3Verify
Stop the process inside the container the hard way and watch the restart count rise.
docker compose up -d
docker container inspect --format '{{.RestartCount}}' my-workerConfirmed by docs.docker.com
Cause 3 of 4
The container crashes before it has run for ten seconds
1Check
A policy applies only after the container has been up for at least 10 seconds and Docker is watching it. This stops a container that can never start from looping. The real fault is then inside the app, so read its log.
docker compose logs --tail 50 api2Fix
Fix the startup error the log shows (missing variable, wrong database host, bad command). Do not raise the policy limits: the policy cannot help a container that never starts.
docker compose up -d
docker compose logs -f api3Verify
The container stays Up past ten seconds.
docker compose psConfirmed by docs.docker.com
Cause 4 of 4
The container was stopped by hand
1Check
A manual stop overrides the policy until the daemon restarts or you start the container again. With unless-stopped, a container you stopped by hand stays stopped even after the daemon comes back; with always it returns only when the daemon restarts.
docker ps -a2Fix
Start it again with Compose. Leave it stopped on purpose only when you mean to.
docker compose up -d3Verify
The service shows as running.
docker compose psConfirmed by docs.docker.com
How do I stop it coming back?
- Put restart: unless-stopped on every long-running service in the Compose file and review it in code review.
- Make jobs that end by design (migrations, one-off tasks) a separate service without a restart policy, or use on-failure:3.
- Do not run systemd or another supervisor to restart containers that already have a Docker restart policy; Docker warns that the two conflict.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Servers
A Docker container keeps restarting and never stays up
docker ps --filter status=restarting lists the container
A container restarts forever when its main process exits and a restart policy starts it again.
3 causes, each with a fixServers
apk cannot find py-pip in an Alpine Docker image
py-pip (no such package):
On current Alpine releases there is no package called py-pip.
2 causes, each with a fixServers
ng serve runs in the container but localhost:4200 does not open
The browser cannot open localhost:4200 although ng serve is running in the container
ng serve listens on localhost by default, which inside a container is the container itself.
3 causes, each with a fixServers
Angular routes return 404 from nginx when you refresh or open a link
The home page loads, but refreshing /some/route shows an nginx 404
Angular routes like /users/42 are not files, so nginx looks for them on disk and returns 404.
3 causes, each with a fixServers
AH00558: Apache in Docker cannot reliably determine its fully qualified domain name
Set the 'ServerName' directive globally to suppress this message
AH00558 is a warning, not a crash.
2 causes, each with a fixServers
Apache in a Docker container logs AH01114 when it proxies to a backend
AH01114: HTTP: failed to make connection to backend: api
Apache's proxy worker could not open a connection to the address in ProxyPass.
5 causes, each with a fix