Docker restart policy: always vs unless-stopped vs on-failure
A restart policy tells Docker whether to start a container again after it exits or after the host reboots, and unless-stopped is the usual choice for services.
- Updated Nov 27, 2026
- Markdown
Make a container start again after a crash or reboot with a restart policy.
- Docker installed on Ubuntu
Set --restart unless-stopped on docker run, or restart: unless-stopped in compose.yaml, and Docker starts the container again after a crash or a reboot, except when you stopped it by hand. The policy also needs the Docker service to start at boot, so check both.
How it works#
Docker keeps a restart policy in each container’s configuration. The Docker daemon watches the container’s main process. When the process exits, the daemon checks the policy and decides whether to start it again. When the daemon itself starts (after a reboot, or a daemon restart), it checks the policy again for every container that is not running.
Two details matter. First, Docker waits between restarts and the wait grows after each failure, so a crashing container does not spin at full speed. It shows in docker ps as Restarting. Second, a policy applies only after the container has stayed up for a short time, so a container that fails instantly at start does not count as a successful start.
Restart policies do not look at health. A container that is hung but whose process is still alive is not restarted. A policy also cannot help if Docker itself is not running, so the Docker service must be enabled at boot too (see Run a service with systemd).
The four policies#
| Policy | Restarts after a crash | Restarts after daemon or host restart | After you stopped it by hand |
|---|---|---|---|
no (default) | No | No | Stays stopped |
on-failure | Yes, only on a non-zero exit code | Depends on how it stopped, so do not rely on it for reboots | Stays stopped |
always | Yes | Yes | Starts again when the daemon starts |
unless-stopped | Yes | Yes | Stays stopped |
You can cap retries on failure, for example on-failure:5.
A worked example, start to finish#
You run docker run -d --name web --restart unless-stopped nginx.
- The nginx process crashes. The daemon sees the exit, checks the policy and starts the container again after a short delay.
- You run
docker stop webfor maintenance. The container stays stopped, because you asked for it. - The server reboots. The Docker service starts at boot. For
unless-stopped,webstays stopped because step 2 stopped it by hand. Withalways, it would start again here. - You run
docker start web. It runs again, and the policy applies to the next crash as before.
Every step comes from one rule: the policy reacts when the process exits or when the daemon starts, and unless-stopped remembers a manual stop.
Step 1: Set a policy#
With docker run:
docker run -d --name web --restart unless-stopped nginxIn compose.yaml:
services:
web:
image: nginx
restart: unless-stoppedApply a change to a Compose file with docker compose up -d.
What you should see: the container starts as usual. The policy shows up in the inspect output in step 4.
Step 2: Change a container that already exists#
docker update --restart unless-stopped webWhat you should see: the container name printed back. A container managed by Compose goes back to the value in the file the next time you run docker compose up -d, so edit the file too.
Step 3: Start Docker at boot#
Policies only work if the Docker service itself runs after a reboot:
sudo systemctl enable dockerThe official Ubuntu packages usually enable it already, but it is worth confirming.
Step 4: Verify the policy#
docker inspect -f '{{.HostConfig.RestartPolicy.Name}}' webWhat you should see: unless-stopped. To check the service:
systemctl is-enabled dockerIt should print enabled. For a real test on a machine you can safely reboot, run sudo reboot, log in again and run docker ps. The container should be listed as Up, with an uptime that started at boot.
To test a crash without rebooting, find the container’s main process on the host with docker top web, then stop it with sudo kill -9 PID. A kill from outside is not a manual docker stop, so the policy restarts the container. Run docker ps a few seconds later and the uptime has reset.
Check it worked#
The inspect command prints the policy you set, systemctl is-enabled docker prints enabled, and after a reboot docker ps lists the container as running without you starting it.
Edge cases and gotchas#
- A crash loop looks like it works.
Restarting (1) 5 seconds agoindocker psmeans the process keeps failing. The policy hides the crash, it does not fix it. Readdocker logs web(see Read container logs). alwaysrevives containers you stopped. Afterdocker stopand a later daemon restart, analwayscontainer starts again. Useunless-stoppedif that surprises you.- Order at boot is not guaranteed. An app can start before its database is ready and crash once, then be restarted. Give the app retry logic, or use healthchecks with
depends_on. - A manual stop is not a crash. Containers stopped with
docker stopdo not come back withon-failureorunless-stoppeduntil you start them. - Out-of-memory kills. A container killed for running out of memory exits with code 137, and a policy then restarts it, which can loop on a small server. Check
docker inspectfor OOMKilled, and add swap or memory. on-failureignores clean exits. A job that exits with 0 is left stopped, which is what you want for one-off tasks.- Compose and
docker updatecan disagree. Changes made withdocker updateare lost when Compose recreates the container from the file.
Troubleshooting#
| Symptom | Likely cause | Fix |
|---|---|---|
Status Restarting (1) and never stays up | The app crashes at start | docker logs web, fix the cause |
| Nothing starts after reboot | Docker service not enabled, or no policy set | sudo systemctl enable docker, and set a policy |
Container stays down after docker stop and a reboot | unless-stopped honours the manual stop | docker start web |
| Container restarts when you wanted it stopped | The policy is always | docker update --restart unless-stopped web |
Policy reverts after docker compose up -d | The compose file says something else | Edit restart: in the file |
| Exit code 137 and repeated restarts | The process was killed, often out of memory | free -h, add swap, or set a memory limit |
invalid restart policy error | A typo in the policy name | Use no, on-failure, always or unless-stopped |
docker ps shows Restarting forever | The process exits at start, so the policy keeps retrying | Read the logs, fix the cause, then recreate the container |
| A container you removed keeps coming back | A Compose project or script recreates it | docker compose down in the right folder, or remove the script |
Real-world scenario#
A VPS provider reboots your server for a kernel update at night. In the morning the website works, because every service in the Compose file has restart: unless-stopped and the Docker service starts at boot. The one exception is a test container you stopped the week before, which stays stopped, as intended.
Another week the app crashes after a bad deploy and loops. docker ps shows Restarting (1) and the site is down, so the policy is hiding a crash. You read the logs, find a missing variable, and fix it. The blog guide Docker container restarting in a loop walks through that diagnosis, and Rollbacks covers going back to the last good image.
Quick reference#
| Policy | Use it for |
|---|---|
no | One-off containers and tests |
on-failure[:N] | Jobs that retry after errors, at most N times |
always | Services that must come back even after a manual stop and daemon restart |
unless-stopped | Most long-running services |
| Task | Command |
|---|---|
| Set on run | docker run -d --restart unless-stopped IMAGE |
| Change an existing container | docker update --restart unless-stopped NAME |
| Read the policy | docker inspect -f '{{.HostConfig.RestartPolicy.Name}}' NAME |
| Enable Docker at boot | sudo systemctl enable docker |
Related lessons#
- Docker healthchecks: detect a hung app that a policy cannot see
- Docker Compose basics: set
restart:per service - Run a service with systemd: how the Docker service starts at boot
- Read container logs: find why a container keeps exiting
Next#
Debug a running container with Run commands inside a container.
Check what you learned
Frequently asked questions
What is the difference between always and unless-stopped?
Both restart a container after a crash or reboot. If you stop the container by hand, unless-stopped keeps it stopped after the next daemon restart, while always starts it again.
Why did my container not start after a reboot?
Either it has no restart policy, or the Docker service itself is not enabled at boot. Check both.
Does a restart policy restart a container that is unhealthy?
No. The policy reacts to the main process exiting, not to a failing healthcheck. A hung but still running process is not restarted by Docker alone.
Which policy should I use for a normal web service?
unless-stopped for most services. Use on-failure for a job that should retry only after errors, and leave no (the default) for one-off containers.
Sources
Keep going: guides and tutorials
- Deploy a Docker Compose App Self-Hosted: Web, Redis and Rollback Deploy a Docker Compose app on your own server with Levelrail: a web service and Redis, a bad release that does not take traffic, and a compose rollback.
- Docker Container Restarting in a Loop: 5 Causes, Fixed Five real reasons a Docker container keeps restarting, each reproduced with its exit code, the command that finds it and the fix. Then the same crash on Levelrail.
- Deploy an Express App to a VPS With Docker and Health Checks Deploy a Node.js Express API to your own server with no Dockerfile. Covers the port rule, a health route, secrets, deploy failure codes and rollback.