liveBook · Manning

Restart policy

As explained in Build and Orchestrator in Go (From Scratch)

container.RestartPolicy is a Docker API configuration structure specifying how a container is handled after termination or failure.

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:

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

How Build and Orchestrator in Go (From Scratch) covers it

How deep it goes
Defines four policy values and their exit or stop behavior, then examines failure ownership in orchestrated systems.
Examples
Uses a Config struct and docker run --restart=on-failure:1 for an echo-server container.
What it assumes
Assumes task and container concepts, then teaches restart behavior through explicit policy values and exit status.
What you can do after
Operate/configure Docker containers with no, on-failure, always, or unless-stopped restart behavior.
How it connects
Connects task configuration with containers, the Docker daemon, orchestration systems, workers, managers, and systemd.

Also explained in

Compare how 3 books explain restart policy →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build and Orchestrator in Go (From Scratch). The section links point to the parts of the book it draws on.