Sub topic 3
The default: no automatic restart
This chapter assumes you can run containers with docker run, list them with docker ps, and read exit codes (0 is success, non-zero is failure). It also assumes you know that a container stops when its main process exits.
What `no` means
Every container has a restart policy. If you don't set one, Docker uses no. With this policy Docker never restarts the container on its own. The same applies whatever the exit code is and whatever the reason for the exit:
- The main process exits with code 0: the container stays stopped.
- The main process crashes with a non-zero code: the container stays stopped.
- The Docker daemon restarts, for example after a host reboot: the container stays stopped.
The container moves to the exited state and stays there until you start it.
Seeing it happen
The following commands start a container that fails at once. They then check what Docker did with it.
docker run -d --name once alpine sh -c 'exit 1'
sleep 3
docker ps -a --filter name=once
docker inspect -f '{{.HostConfig.RestartPolicy.Name}} {{.RestartCount}} {{.State.Status}}' onceThe docker ps -a output shows Exited (1). The docker inspect line prints no 0 exited. That means the policy is no, Docker has restarted the container 0 times, and its status is exited. On older Docker versions the policy name may print as an empty string. An empty string also means no.
You need -a with docker ps because plain docker ps lists only running containers. A stopped container with the no policy is easy to overlook if you forget this.
Setting `no` explicitly
Because no is the default, these two commands behave the same:
docker run -d --name plain nginx:alpine
docker run -d --name explicit --restart no nginx:alpineWriting it out helps readers of a script or Compose file see that the choice was deliberate. You can also switch an existing container back to no without recreating it:
docker update --restart no webdocker update changes only the policy. It does not stop or start the container.
Bringing a container back by hand
To run a stopped container again, start it yourself:
docker start onceThis runs the same container with its original command. The command above fails again and the container returns to exited.
When `no` is the right choice
The no policy fits workloads that should run once and finish, or that you want to control yourself:
- One-off jobs such as migrations, backups, and build steps.
- Interactive containers started with
docker run -it. - Development and debugging. An automatic restart can hide a crash that you want to see.
Use another policy when a service must stay up without anyone watching it. The next chapters cover those policies.
Common mistakes
- Expecting recovery after a reboot. A container with
nodoes not come back when the host restarts. After a reboot,docker psmay look empty, anddocker ps -ashows the containers as exited. - Combining
--rmwith a restart policy.--rmdeletes the container when it exits. Docker rejects--rmtogether with--restart(other thanno), because a restart policy needs the container to still exist. - Reading
exitedas a Docker fault. Docker did what the policy told it to do. Check the exit code and the logs withdocker logs onceto find out why the process ended.
Exercise
You run docker run -d --name job alpine sh -c 'exit 2' and wait a few seconds. What does docker ps show for job, what does docker ps -a show, and what single command runs it again?
Show answer
docker ps shows nothing for job, because it lists only running containers and the default policy no leaves the container stopped. docker ps -a shows it as Exited (2). docker start job runs it again, and it exits with code 2 again.