Sub topic 8
Declaring restart policies in Docker Compose
This chapter assumes you can run containers with docker run, read basic YAML, and know the four restart policies: no, on-failure, always and unless-stopped. It also assumes you have Docker Compose v2 installed (the docker compose command).
The service-level restart key
In a Compose file, each service can have a restart key. Its value is one of the same four policies you pass to docker run --restart. Compose passes the value to Docker when it creates the container, so the behavior is identical.
services:
web:
image: nginx:alpine
restart: unless-stopped
worker:
image: alpine
command: ["sh", "-c", "exit 1"]
restart: on-failure:3Here web restarts after crashes and after a daemon restart, unless you stopped it yourself. worker always exits with code 1, so Docker retries it at most three times and then leaves it stopped.
The maximum retry count uses the same on-failure:N syntax as docker run.
Quote the value `no`
If you omit restart, the policy is no. If you want to write it out, quote it:
services:
batch:
image: alpine
command: ["echo", "done"]
restart: "no"YAML 1.1 parsers read an unquoted no as the boolean false. Compose may reject the file or silently treat the value in a way you did not intend. Quoting makes the value a string.
Applying and checking the policy
Start the stack, then inspect each container. The docker compose ps -q command prints the container ID for a service, which you can pass to docker inspect.
docker compose up -d
docker inspect -f '{{.HostConfig.RestartPolicy.Name}} {{.RestartCount}} {{.State.Status}}' $(docker compose ps -q worker)After a few seconds, the output for worker shows on-failure, a restart count of 3, and a status of exited.
Changing a policy in an existing project
The policy is part of the container's configuration. If you edit restart in the Compose file and run docker compose up -d again, Compose notices that the configuration changed and recreates the affected service's container. Any state stored inside the container's writable layer is lost, though volumes are kept.
If you only want to change the policy of a running container without recreating it, use docker update:
docker update --restart no $(docker compose ps -q web)This change is not written back to the Compose file. The next time Compose recreates the container, the file's value wins. Treat the Compose file as the source of truth and use docker update for temporary changes.
Compose and a stopped project
docker compose stop and docker compose down are manual stops. Under unless-stopped, a container stopped this way stays stopped after a daemon restart. Under always, Docker starts it again when the daemon restarts. This matches what you saw for docker stop earlier in the tutorial.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Service never restarts | `restart` is missing, so the policy is `no` | Add `restart:` to the service |
| `restart: no` behaves oddly | Value was not quoted | Use `restart: "no"` |
| Edited policy has no effect | Container was not recreated | Run `docker compose up -d` again |
| Policy reverts after `docker update` | Compose recreated the container | Edit the Compose file instead |
Deploy-time restart settings
Compose files also have a deploy.restart_policy section. It is used by Docker Swarm and has different keys. A later chapter covers it. For a single host with docker compose up, use the service-level restart key shown here.
Exercise
You set restart: unless-stopped on a service, run docker compose up -d, and later change the value to on-failure:5 in the file. What must you do for the new policy to apply, and what happens to data in a named volume?
Show answer
Run docker compose up -d again. Compose detects the changed configuration and recreates the container with the new policy. Data in a named volume is kept, because volumes are not removed when a container is recreated.
Exercise
Why should you write restart: "no" with quotes in a Compose file?
Show answer
Many YAML parsers read an unquoted no as the boolean false, not the string no. Quoting guarantees that Compose receives the string no.