feat(docker): opt-in boot and shutdown units for hosts that run Docker with live-restore #369

Open
opened 2026-11-25 16:47:33 +00:00 by Dragonish · 0 comments
Collaborator

Problem

On a host that runs Docker with live-restore (so that a docker.service restart leaves containers running), two things go wrong at a reboot of the whole host. Both are about Docker and only matter on a host that runs containers; neither belongs in system_baseline.

  1. A container is not started at boot. A reboot kills the containers with the host. At the next boot dockerd's restore pass sometimes skips one without trying: the container's restart policy is unless-stopped or always, RestartCount stays 0, Docker reports it as exited with code 255 (the code for "the process vanished without Docker seeing it exit"), and nothing starts it. Seen twice on two different hosts (a reverse proxy container, then a metrics and a blackbox container). The service stays down until someone notices.
  2. A slow shutdown when a container holds an NFS mount. Because of live-restore, dockerd does not stop containers at shutdown. systemd-shutdown signals them only after the network is already stopped. A container that bind-mounts an NFS share then sits in uninterruptible sleep in the kernel's final unmount (nfs4_destroy_session, waiting on a DESTROY_SESSION request to a server it can no longer reach), so SIGTERM seems ignored and SIGKILL does not help until systemd's own timer fires. Measured on a VM with four containers and two NFS shares: 181.6 s to shut down (the hypervisor then killed it), against 8.1 to 8.3 s with the fix below.

Out of scope: daemon.json settings (a separate issue), system_baseline, and changing the restart policy of any container.

Proposal

Two opt-in inputs on the docker role, both default false, so a host that does not set them is unchanged:

  • docker_start_missed (bool): installs /usr/local/sbin/docker-start-missed and a unit docker-start-missed.service (After=docker.service, Type=simple, WantedBy=multi-user.target, so it never delays or fails the boot). Once per boot it waits docker_start_missed_settle_seconds (default 30) for dockerd's own restarts to finish, then starts a container only if all of these hold: the restart policy is unless-stopped or always; it is exited with code 255 (a manual docker stop gives 0, 137 or 143); it finished after this boot began. A container stopped on purpose therefore stays stopped. It uses docker start, never changes policy.
  • docker_stop_at_shutdown (bool, with docker_stop_at_shutdown_timeout, default 30): installs a script and a RemainAfterExit oneshot unit ordered After=network-online.target remote-fs.target and Before=docker.service. Its ExecStop runs at shutdown after docker.service has stopped and before the network: it sends SIGTERM to every process in the container cgroup scopes (/sys/fs/cgroup/system.slice/docker-*.scope), waits up to the timeout, then SIGKILL, and logs what it did. It never calls docker stop or docker kill, because both set the manually-stopped marker and a container with unless-stopped would then not start at the next boot. The unit is started on the first run (systemd runs ExecStop of a RemainAfterExit unit only if it is active), except in check mode.

"Only on a host that uses Docker": the units are part of the docker role, so they exist only on a host the caller applies that role to. The role installs them only after Docker itself is installed, both units get ConditionPathExists=/usr/bin/docker so they do nothing if the binary is gone (new in this proposal; the local copy does not have it yet), and docker_remove: true removes both units and scripts together with Docker. Setting an input back to false removes that unit and script, not just disables it.

I wrote and run both in a local system_baseline copy and will port them with Molecule coverage for install, idempotence, switch-off and removal. I will not open a PR before this issue is Status/Ready for Agent.

Acceptance criteria

  • With both inputs at their defaults, a run changes nothing and creates no unit or script. Check: Molecule idempotence and a file-absence verify.
  • docker_start_missed: true installs the script and unit (mode 0755 / 0644), enables the unit without a handler flush, and a second run reports no change. Check: Molecule verify.yml and the idempotence step.
  • The start-missed logic starts only a container with policy unless-stopped or always, exit code 255, finished after boot, and leaves one exited with 0, 137 or 143, or with policy no, alone. Check: unit tests of the selection function on docker inspect samples, including the nanosecond timestamp format.
  • docker_stop_at_shutdown: true installs the script and unit with the stated ordering and starts the unit outside check mode. Check: systemctl show -p After -p Before docker-stop-at-shutdown.service in verify, and systemctl is-active.
  • A shutdown with a container that holds an NFS mount takes seconds, not minutes, and the journal has docker-stop-at-shutdown lines with the process count. Check: a manual run on a throwaway VM (Molecule's container has no systemd or NFS; stated in the PR as not verified there).
  • The shutdown script never runs docker stop or docker kill. Check: grep in the test suite.
  • Switching either input back to false removes its unit, symlink and script; docker_remove: true removes both. Check: Molecule converge with the input flipped, then verify.
  • argument_specs.yml, the README (with a short "when you need this" section) and a changelog fragment (minor_changes) cover both inputs. Check: ansible-doc, antsibull-changelog lint.

Assisted-by: Claude Code

## Problem On a host that runs Docker with `live-restore` (so that a `docker.service` restart leaves containers running), two things go wrong at a reboot of the whole host. Both are about Docker and only matter on a host that runs containers; neither belongs in `system_baseline`. 1. **A container is not started at boot.** A reboot kills the containers with the host. At the next boot dockerd's restore pass sometimes skips one without trying: the container's restart policy is `unless-stopped` or `always`, `RestartCount` stays 0, Docker reports it as exited with code 255 (the code for "the process vanished without Docker seeing it exit"), and nothing starts it. Seen twice on two different hosts (a reverse proxy container, then a metrics and a blackbox container). The service stays down until someone notices. 2. **A slow shutdown when a container holds an NFS mount.** Because of `live-restore`, dockerd does not stop containers at shutdown. systemd-shutdown signals them only after the network is already stopped. A container that bind-mounts an NFS share then sits in uninterruptible sleep in the kernel's final unmount (`nfs4_destroy_session`, waiting on a `DESTROY_SESSION` request to a server it can no longer reach), so SIGTERM seems ignored and SIGKILL does not help until systemd's own timer fires. Measured on a VM with four containers and two NFS shares: 181.6 s to shut down (the hypervisor then killed it), against 8.1 to 8.3 s with the fix below. Out of scope: `daemon.json` settings (a separate issue), `system_baseline`, and changing the restart policy of any container. ## Proposal Two opt-in inputs on the `docker` role, both default `false`, so a host that does not set them is unchanged: - `docker_start_missed` (bool): installs `/usr/local/sbin/docker-start-missed` and a unit `docker-start-missed.service` (`After=docker.service`, `Type=simple`, `WantedBy=multi-user.target`, so it never delays or fails the boot). Once per boot it waits `docker_start_missed_settle_seconds` (default 30) for dockerd's own restarts to finish, then starts a container only if all of these hold: the restart policy is `unless-stopped` or `always`; it is exited with code 255 (a manual `docker stop` gives 0, 137 or 143); it finished after this boot began. A container stopped on purpose therefore stays stopped. It uses `docker start`, never changes policy. - `docker_stop_at_shutdown` (bool, with `docker_stop_at_shutdown_timeout`, default 30): installs a script and a `RemainAfterExit` oneshot unit ordered `After=network-online.target remote-fs.target` and `Before=docker.service`. Its `ExecStop` runs at shutdown after `docker.service` has stopped and before the network: it sends SIGTERM to every process in the container cgroup scopes (`/sys/fs/cgroup/system.slice/docker-*.scope`), waits up to the timeout, then SIGKILL, and logs what it did. It never calls `docker stop` or `docker kill`, because both set the manually-stopped marker and a container with `unless-stopped` would then not start at the next boot. The unit is started on the first run (systemd runs `ExecStop` of a `RemainAfterExit` unit only if it is active), except in check mode. "Only on a host that uses Docker": the units are part of the `docker` role, so they exist only on a host the caller applies that role to. The role installs them only after Docker itself is installed, both units get `ConditionPathExists=/usr/bin/docker` so they do nothing if the binary is gone (new in this proposal; the local copy does not have it yet), and `docker_remove: true` removes both units and scripts together with Docker. Setting an input back to `false` removes that unit and script, not just disables it. I wrote and run both in a local `system_baseline` copy and will port them with Molecule coverage for install, idempotence, switch-off and removal. I will not open a PR before this issue is `Status/Ready for Agent`. ## Acceptance criteria - [ ] With both inputs at their defaults, a run changes nothing and creates no unit or script. Check: Molecule idempotence and a file-absence verify. - [ ] `docker_start_missed: true` installs the script and unit (mode 0755 / 0644), enables the unit without a handler flush, and a second run reports no change. Check: Molecule `verify.yml` and the idempotence step. - [ ] The start-missed logic starts only a container with policy `unless-stopped` or `always`, exit code 255, finished after boot, and leaves one exited with 0, 137 or 143, or with policy `no`, alone. Check: unit tests of the selection function on `docker inspect` samples, including the nanosecond timestamp format. - [ ] `docker_stop_at_shutdown: true` installs the script and unit with the stated ordering and starts the unit outside check mode. Check: `systemctl show -p After -p Before docker-stop-at-shutdown.service` in verify, and `systemctl is-active`. - [ ] A shutdown with a container that holds an NFS mount takes seconds, not minutes, and the journal has `docker-stop-at-shutdown` lines with the process count. Check: a manual run on a throwaway VM (Molecule's container has no systemd or NFS; stated in the PR as not verified there). - [ ] The shutdown script never runs `docker stop` or `docker kill`. Check: grep in the test suite. - [ ] Switching either input back to `false` removes its unit, symlink and script; `docker_remove: true` removes both. Check: Molecule converge with the input flipped, then verify. - [ ] `argument_specs.yml`, the README (with a short "when you need this" section) and a changelog fragment (`minor_changes`) cover both inputs. Check: `ansible-doc`, `antsibull-changelog lint`. Assisted-by: Claude Code
Dragonish added the
Status
Ready for Human
label 2026-11-25 16:47:33 +00:00
Dragonish added
Status
Ready for Agent
and removed
Status
Ready for Human
labels 2026-11-25 16:54:27 +00:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: Mortis/Kamino#369