Sub topic 15
Restart policies and running multiple replicas
This chapter assumes you can read a compose.yaml, know how Compose networks and published ports work, and have used health checks and depends_on. It covers how Compose restarts failed containers and how to run more than one container for a service.
Restart policies
By default a container that exits stays stopped. The restart key on a service changes that. Docker Engine applies the policy, not Compose, so it also applies after a Docker daemon restart.
| Value | Behavior |
|---|---|
| `no` | Never restart. This is the default. |
| `always` | Restart whenever the container stops. If you stop it by hand, it starts again when the daemon restarts. |
| `on-failure[:max]` | Restart only on a non-zero exit code. Optional retry limit. |
| `unless-stopped` | Like `always`, but a container you stopped stays stopped after a daemon restart. |
services:
web:
build: .
restart: unless-stopped
worker:
build: ./worker
restart: on-failure:5Use unless-stopped for long-running services. Use on-failure with a limit for jobs where an endless crash loop would hide a bug.
A restart policy reacts to the process exiting. It does not react to a failing health check. An unhealthy container keeps running, so the policy never fires. Health checks only affect depends_on conditions and what docker compose ps reports.
services:
web:
build: .
restart: unless-stopped
depends_on:
db:
condition: service_healthy
restart: trueWith restart: true under a dependency, Compose restarts web when it restarts db through a Compose command such as docker compose restart db. This is separate from the service's own restart policy.
Running multiple replicas
You can run several containers from one service definition. Use --scale on the command line:
docker compose up -d --scale web=3
docker compose psYou can also set deploy.replicas in the file:
services:
web:
image: myapp:latest
deploy:
replicas: 3The command-line flag overrides the file value. Compose names the containers shop-web-1, shop-web-2, and shop-web-3.
Limits of scaling in Compose
Replicas share the service's definition, so some settings break at more than one copy:
- A fixed host port such as
"8000:8000"fails for the second replica, because only one container can bind that host port. Compose reports a port conflict. - A fixed
container_namefails, because container names must be unique. - Replicas that share one named volume write to the same files. That is unsafe for something like a database.
For ports, publish only the container port and let Docker pick a free host port, or give a range:
services:
web:
image: myapp:latest
ports:
- "8000-8010:8000"Inside the Compose network, the service name web resolves to the addresses of all replicas. Docker's DNS returns them in rotating order, which spreads connections roughly evenly. That is DNS round-robin, not a load balancer. It does not check health, and clients that cache a DNS answer keep using one replica. For real load balancing, put a reverse proxy such as Nginx or Traefik in front of the replicas.
Common errors
- Expecting
restartto react to an unhealthy container. It only reacts to exits. - Scaling a service that has a fixed host port or
container_name. - Using
restart: alwayson a container that crashes at startup. It loops forever and fills the logs. Check withdocker compose logs.
Exercise
A service has restart: unless-stopped and a health check that starts failing, but the process keeps running. Does Docker restart the container? What happens if you run docker compose up -d --scale web=2 when web publishes "8000:8000"?
Show answer
No. A restart policy only reacts to the process exiting, and an unhealthy container is still running. The scale command fails for the second replica because both containers would bind host port 8000. Publish a port range or only the container port, or put a proxy in front.
Exercise
Which restart policy fits a batch worker that should retry a few times after a crash and then stop?
Show answer
on-failure:5 (or another small maximum). It restarts only on a non-zero exit and gives up after the limit.