Chapter 2 · Lesson 11 of 20
Docker

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.

By the end you will

Make a container start again after a crash or reboot with a restart policy.

Time
20 min
Level
Beginner
Check
4 questions
You need
  • 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#

PolicyRestarts after a crashRestarts after daemon or host restartAfter you stopped it by hand
no (default)NoNoStays stopped
on-failureYes, only on a non-zero exit codeDepends on how it stopped, so do not rely on it for rebootsStays stopped
alwaysYesYesStarts again when the daemon starts
unless-stoppedYesYesStays 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 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).
  • 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 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#

SymptomLikely causeFix
Status Restarting (1) and never stays upThe app crashes at startdocker logs web, fix the cause
Nothing starts after rebootDocker service not enabled, or no policy setsudo systemctl enable docker, and set a policy
Container stays down after docker stop and a rebootunless-stopped honours the manual stopdocker start web
Container restarts when you wanted it stoppedThe policy is alwaysdocker update --restart unless-stopped web
Policy reverts after docker compose up -dThe compose file says something elseEdit restart: in the file
Exit code 137 and repeated restartsThe process was killed, often out of memoryfree -h, add swap, or set a memory limit
invalid restart policy errorA typo in the policy nameUse no, on-failure, always or unless-stopped
docker ps shows Restarting foreverThe process exits at start, so the policy keeps retryingRead the logs, fix the cause, then recreate the container
A container you removed keeps coming backA Compose project or script recreates itdocker 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#

PolicyUse it for
noOne-off containers and tests
on-failure[:N]Jobs that retry after errors, at most N times
alwaysServices that must come back even after a manual stop and daemon restart
unless-stoppedMost long-running services
TaskCommand
Set on rundocker run -d --restart unless-stopped IMAGE
Change an existing containerdocker update --restart unless-stopped NAME
Read the policydocker inspect -f '{{.HostConfig.RestartPolicy.Name}}' NAME
Enable Docker at bootsudo systemctl enable docker

Next#

Debug a running container with Run commands inside a container.

Check what you learned4 questions
1You stop a container by hand and later restart the Docker daemon. Which policy keeps it stopped?
2Containers have a restart policy but nothing starts after a reboot. What else should you check?
3A container keeps showing Restarting. What does the restart policy do about the crash?
4A container with restart on-failure finishes its work and exits with code 0. What happens?

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

View MD Edit
Share
Helpful?
Progress is saved in this browser only.

Keep going: guides and tutorials

View MD Edit
Share
Helpful?
Run this on your own server

Levelrail deploys apps from git with health-checked releases and one-click rollback.

docker pull ghcr.io/glincker/levelrail:latest Install Levelrail From GLINR Studios