A restart policy tells Docker what to do when a container terminates. It belongs in task configuration alongside resource requirements, environment variables, and the container image, providing one mechanism for resilience. section 1.5.1 section 3.3
Choose behavior according to the container’s exit condition:
- no, represented by an empty value or the default: do nothing when the container exits. section 3.3 section 9.3.3
- on-failure: restart the container when it exits with a nonzero status code. section 3.3 section 9.3.3
- always: restart the container regardless of its exit code. section 3.3 section 9.3.3
- unless-stopped: restart the container unless a user explicitly stopped it. section 3.3 section 9.3.3
Task configuration
A task configuration can store the policy directly: Config includes a RestartPolicy string alongside Image, Cpu, Memory, Disk, and Env. The equivalent command-line setting is docker run --restart=on-failure:1, which asks Docker to restart an echo-server container once after a failure. section 3.3 section 9.3.3
Restart policies work well for individual containers, but they can blur responsibility in an orchestrated system. Docker, systemd, and the orchestrator may each appear responsible for recovery. A simplified orchestrator can therefore handle task failures itself rather than relying on Docker’s policy, keeping failure decisions within its own control. section 9.3.3
Listing 3.6 The Config struct that will hold the configuration for orchestration tasks
type Config struct {
Name string
AttachStdin bool
AttachStdout bool
AttachStderr bool
ExposedPorts nat.PortSet
Cmd []string
Image string
Cpu float64
Memory int64
Disk int64
Env []string
RestartPolicy string
}
Inspect the task configuration structure and see where RestartPolicy sits among the container’s runtime settings. section 3.3
$ docker run \ --restart=on-failure:1 \ --name echo \ -p 7777:7777 \ -it \ timboring/echo-server:bad-exit
Inspect a concrete Docker command that applies an on-failure restart policy to an echo-server container. section 9.3.3


