Skip to content

daemon: add configurable default restart policy - #53993

Open
reboss wants to merge 1 commit into
moby:masterfrom
reboss:default-restart-policy
Open

reboss wants to merge 1 commit into
moby:masterfrom
reboss:default-restart-policy

Conversation

@reboss

@reboss reboss commented Oct 20, 2026

Copy link
Copy Markdown

Summary

This PR adds daemon-level default restart policy configuration through two new options:

  • default-restart-policy: Sets a default restart policy for containers when no explicit policy is specified
  • enforce-default-restart-policy: When enabled, rejects container creation with a different restart policy

Problem

In resource-managed environments, containers with restart policies can survive external process cleanup, causing resource leaks:

docker run --restart unless-stopped \
  --cgroup-parent=/managed/scope/job_12345 \
  busybox sleep 1000

When the resource manager terminates processes in the cgroup, Docker's restart policy races with cgroup cleanup. If Docker restarts the container before the cgroup is destroyed, the container survives and consumes unaccounted resources.

Solution

Cluster operators can configure the daemon to enforce restart=no on all containers:

{
  "default-restart-policy": {"Name": "no"},
  "enforce-default-restart-policy": true
}

This prevents the restart race by rejecting containers that specify restart policies.

Behavior:

  • Enforcement disabled (default): containers can override the daemon default
  • Enforcement enabled: container creation fails if a conflicting policy is specified
  • Container updates not modifying restart policy are always allowed

AI Disclosure: This PR was developed with substantial assistance from Claude Code (Anthropic). AI was used for:

  • Initial design and implementation (~70% of code)
  • Test case development
  • Documentation and commit messages

All AI-generated content was reviewed, edited, and tested by the human maintainer.

Release notes

Add `default-restart-policy` and `enforce-default-restart-policy` daemon config options to control container restart policies cluster-wide.

Add daemon configuration options to set a default restart policy for
containers and optionally enforce it.

New configuration options in daemon.json:

- default-restart-policy: Sets the default restart policy for containers
  that don't specify one. Defaults to "no" for backward compatibility.

- enforce-default-restart-policy: When enabled, prevents containers from
  using a different restart policy than the daemon default. Requests with
  a non-matching policy are rejected with an error.

Example daemon.json configuration:

    {
      "default-restart-policy": {
        "Name": "no"
      },
      "enforce-default-restart-policy": true
    }

This provides operators with centralized control over container restart
behavior, addressing use cases where:

- Organizational policy requires specific restart behavior
- External resource management systems have expectations about process
  lifecycle (e.g., batch schedulers, orchestrators, cgroup managers)
- Security policies mandate controlled container restart behavior
- Multi-tenant environments need to prevent certain restart policies

Related to moby#52675 (daemon-managed container default profiles)

Signed-off-by: John Robbins <John.Robbins@amd.com>
Comment thread daemon/config/config.go
Comment on lines +203 to +206
// DefaultRestartPolicy is the default restart policy for containers that
// do not have a container-specific restart policy set. If not specified,
// defaults to "no" (RestartPolicyDisabled).
DefaultRestartPolicy containertypes.RestartPolicy `json:"default-restart-policy,omitempty"`

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should add a specific setting for this. This could be handled via the general mechanism of #52675

Comment thread daemon/config/config.go
Comment on lines +208 to +211
// EnforceDefaultRestartPolicy, when enabled, prevents users from overriding
// the default restart policy. Useful for batch scheduling systems (e.g., SLURM)
// where restart policies conflict with cgroup lifecycle management.
EnforceDefaultRestartPolicy bool `json:"enforce-default-restart-policy,omitempty"`

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is too narrow as a daemon-wide configuration. The real need is the possibility to perform arbitrary checks against the containers.

We should expose this as an extension point, see: #54002

@thaJeztah thaJeztah left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want to have a closer look at the overall design (around defaults, and potentially "templates / profiles"). There's been prior discussions around this (also with, e.g. the host.docker.internal magic domain) around changing daemon default behavior, as in some cases it could change the behavior between daemons (resulting in "it worked on my machine, but failed in prod").

restart-policies especialy should be looked at in this case; if a default means (e.g.) "all containers get a always restart policy", this could potentially cause unexpected results. Note that the daemon used to have a --restart flag, which would restart all containers on start; this was deprecated when restart-policies were added. There's also still problems with the restart-policy overall design (#18473 (comment)) that we should look into.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants