# 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.

URL: https://levelrail.com/learn/docker/docker-restart-policies/
Updated: 2026-11-27

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](https://levelrail.com/learn/server-basics/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`.

1. The nginx process crashes. The daemon sees the exit, checks the policy and starts the container again after a short delay.
2. You run `docker stop web` for maintenance. The container stays stopped, because you asked for it.
3. The server reboots. The Docker service starts at boot. For `unless-stopped`, `web` stays stopped because step 2 stopped it by hand. With `always`, it would start again here.
4. 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`:

```bash
docker run -d --name web --restart unless-stopped nginx
```

In `compose.yaml`:

```yaml
services:
  web:
    image: nginx
    restart: unless-stopped
```

Apply a change to a [Compose](https://levelrail.com/learn/docker/docker-compose-basics/)) 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

```bash
docker update --restart unless-stopped web
```

What 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:

```bash
sudo systemctl enable docker
```

The official Ubuntu packages usually enable it already, but it is worth confirming.

## Step 4: Verify the policy

```bash
docker inspect -f '{{.HostConfig.RestartPolicy.Name}}' web
```

What you should see: `unless-stopped`. To check the service:

```bash
systemctl is-enabled docker
```

It 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 ago` in `docker ps` means the process keeps failing. The policy hides the crash, it does not fix it. Read `docker logs web` (see [Read container logs](https://levelrail.com/learn/deploy-operate/read-container-logs/))).
- **`always` revives containers you stopped.** After `docker stop` and a later daemon restart, an `always` container starts again. Use `unless-stopped` if 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](https://levelrail.com/learn/docker/docker-healthchecks/)) with `depends_on`.
- **A manual stop is not a crash.** Containers stopped with `docker stop` do not come back with `on-failure` or `unless-stopped` until 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 inspect` for OOMKilled, and add swap or memory.
- **`on-failure` ignores clean exits.** A job that exits with 0 is left stopped, which is what you want for one-off tasks.
- **Compose and `docker update` can disagree.** Changes made with `docker update` are 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](https://levelrail.com/blog/docker-container-restarting-in-a-loop/)) walks through that diagnosis, and [Rollbacks](https://levelrail.com/learn/deploy-operate/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](https://levelrail.com/learn/docker/docker-healthchecks/)): detect a hung app that a policy cannot see
- [Docker Compose basics](https://levelrail.com/learn/docker/docker-compose-basics/)): set `restart:` per service
- [Run a service with systemd](https://levelrail.com/learn/server-basics/run-a-service-with-systemd/)): how the Docker service starts at boot
- [Read container logs](https://levelrail.com/learn/deploy-operate/read-container-logs/)): find why a container keeps exiting

## Next

Debug a running container with [Run commands inside a container](https://levelrail.com/learn/docker/run-commands-inside-a-container/)).

## 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

- [Docker docs, start containers automatically](https://docs.docker.com/engine/containers/start-containers-automatically/)
