Knowledgebase

Find frequently asked questions and setup guides by searching or by browsing the categories.

Set a Restart Policy

docker run -d --name web --restart unless-stopped -p 8080:80 nginx:1.30

With this policy, Docker starts the container again when it exits for any reason and when the daemon starts, for example after a reboot, unless you stopped it yourself. Without --restart, the policy is no, and a container that crashes or is stopped by a reboot stays down.

The Four Policies

PolicyRestarts the container
noNever (the default)
on-failureWhen it exits with an error code
on-failure:5The same, at most 5 times
alwaysWhenever it stops
unless-stoppedWhenever it stops, unless you stopped it

After a Manual Stop and After a Reboot

The policies differ in what happens when the Docker daemon starts again, after a reboot or systemctl restart docker:

PolicyWhen the daemon starts
noStays stopped
on-failureStays stopped
alwaysStarts, even if you stopped it
unless-stoppedStarts, unless you stopped it

This follows Docker's own table, which says on-failure does not restart a container when the daemon restarts. A container you stop with docker stop stays stopped while the daemon runs, whatever its policy. The difference shows at the next daemon start: always brings it back, unless-stopped leaves it down. That is why unless-stopped suits most services: a container you switched off on purpose does not return after the next reboot.

Change the Policy of an Existing Container

docker update --restart unless-stopped web
docker inspect -f '{{.HostConfig.RestartPolicy.Name}}' web

docker update changes the policy at once, without restarting the container, and accepts several names in one command. It can also change CPU, memory, and process limits, but not ports, volumes, or environment variables; for those, replace the container as in how to update a Docker container. To change all running containers at once, run docker update --restart unless-stopped $(docker ps -q).

The restart Key in Docker Compose

services:
  web:
    image: nginx:1.30
    restart: unless-stopped
  worker:
    image: myapp:1.0
    restart: on-failure:5
  migrate:
    image: myapp:1.0
    restart: "no"

Quote "no": without quotes, YAML can read it as false. After you change the key, docker compose up -d recreates the affected containers; see docker compose up -d.

Start Docker Itself at Boot

A restart policy works only if the Docker daemon starts. Check that the service is enabled:

systemctl is-enabled docker
sudo systemctl enable docker

The Debian and Ubuntu packages enable it during installation; on AlmaLinux and Rocky Linux run sudo systemctl enable --now docker. Service states are explained in how to list services with systemctl.

Restart Loops and Back-Off

A container that keeps failing is restarted again and again. Docker waits longer before each attempt: the delay starts at 100 milliseconds and doubles each time, up to one minute, and goes back to the start once the container has run for at least 10 seconds. docker ps shows such a container as Restarting. Find the cause in its output with docker logs, and check the count with docker inspect -f '{{.RestartCount}}' web.

A Restart Policy Is Not a Health Check

Docker restarts a container only when its main process exits. A program that hangs, or answers every request with an error, keeps running, and the policy does nothing. A HEALTHCHECK in the Dockerfile marks such a container as unhealthy, but the restart policy reacts only to the process exiting, so an unhealthy container keeps running. Watch the service from outside, for example with a check that requests a page and alerts you when it fails.

A restart policy cannot be combined with --rm, which deletes the container when it exits; see how to stop and remove Docker containers.

Was this answer helpful?
Loading...