feat(docker): add opt-in boot and shutdown units for hosts that run Docker with live-restore
Build / build (pull_request) Successful in 27s
Lint / lint (pull_request) Successful in 2m15s
Molecule / molecule (pull_request) Successful in 4m17s

docker_start_missed installs a boot unit that starts the containers dockerd's
restore pass skipped after a reboot of the whole host. docker_stop_at_shutdown
installs a unit that ends container processes at shutdown while the network and
NFS shares are still up, so a container holding an NFS mount no longer stalls
the shutdown. Both default to false and do nothing without /usr/bin/docker.

Refs #369

Assisted-by: Claude Code
This commit is contained in:
Dragonish committed 2026-11-25 17:31:37 +00:00
1 parent 4a7189c067
commit 567e9cca7c
16 files changed
+917 -6

No files matched your search

+5
View File
@@ -31,5 +31,10 @@
when: not (docker_remove | default(false) | bool)
tags: [docker, configure]
- name: Install or remove the boot and shutdown units
ansible.builtin.include_tasks: units.yml
when: not (docker_remove | default(false) | bool)
tags: [docker, configure]
- name: Flush handlers
ansible.builtin.meta: flush_handlers
+14
View File
@@ -8,6 +8,20 @@
enabled: false
tags: [docker, remove]
- name: Take out the start-at-boot unit
ansible.builtin.include_tasks: unit_absent.yml
vars:
__docker_unit_name: docker-start-missed
__docker_unit_script: /usr/local/sbin/docker-start-missed
tags: [docker, remove]
- name: Take out the stop-at-shutdown unit
ansible.builtin.include_tasks: unit_absent.yml
vars:
__docker_unit_name: docker-stop-at-shutdown
__docker_unit_script: /usr/local/sbin/docker-stop-at-shutdown
tags: [docker, remove]
- name: Remove Docker Engine packages
become: true
ansible.builtin.apt:
+35
View File
@@ -0,0 +1,35 @@
---
# Takes one of the role's units out: disable it, delete its files, tell systemd. A running unit is not stopped, because stopping
# docker-stop-at-shutdown runs its stop action and ends every container. Needs __docker_unit_name and __docker_unit_script.
- name: Disable {{ __docker_unit_name }}
become: true
ansible.builtin.systemd:
name: "{{ __docker_unit_name }}.service"
enabled: false
register: __docker_unit_disabled
# The unit may not exist (systemd: "Could not find the requested service"): there is nothing to disable.
failed_when:
- __docker_unit_disabled.failed | default(false)
- "'Could not find the requested service' not in (__docker_unit_disabled.msg | default(''))"
when: not ansible_check_mode
tags: [docker, configure, remove]
- name: Remove the files of {{ __docker_unit_name }}
become: true
ansible.builtin.file:
path: "{{ item }}"
state: absent
loop:
- "/etc/systemd/system/{{ __docker_unit_name }}.service"
- "{{ __docker_unit_script }}"
register: __docker_unit_removed
tags: [docker, configure, remove]
- name: Reload systemd after removing {{ __docker_unit_name }}
become: true
ansible.builtin.systemd:
daemon_reload: true
when:
- __docker_unit_removed is changed
- not ansible_check_mode
tags: [docker, configure, remove]
+123
View File
@@ -0,0 +1,123 @@
---
# The two opt-in units for a host that runs Docker with live-restore. Each is installed when its input is true and removed when it is false.
- name: Install the boot unit that starts containers Docker skipped
when: docker_start_missed | bool
tags: [docker, configure]
block:
- name: Install the script that starts containers Docker skipped at boot
become: true
ansible.builtin.copy:
src: docker-start-missed
dest: /usr/local/sbin/docker-start-missed
owner: root
group: root
mode: "0755"
tags: [docker, configure]
# Type=simple, not oneshot: a oneshot unit in multi-user.target holds the boot until it finishes, which can take minutes. A failure
# still shows in `systemctl --failed`, and the target only Wants this unit, so it never fails the boot. No Wants= on Docker: a host
# whose operator keeps Docker disabled must not get it started by this unit.
- name: Install the unit that starts containers Docker skipped at boot
become: true
ansible.builtin.copy:
dest: /etc/systemd/system/docker-start-missed.service
owner: root
group: root
mode: "0644"
content: |
[Unit]
Description=Start containers that Docker skipped at boot
After={{ docker_service }}.service
ConditionPathExists=/usr/bin/docker
[Service]
Type=simple
ExecStart=/usr/local/sbin/docker-start-missed --settle {{ docker_start_missed_settle_seconds }}
[Install]
WantedBy=multi-user.target
register: __docker_start_missed_unit
tags: [docker, configure]
# Not in check mode: on the first run the unit file above is only simulated, so systemd would not find it.
- name: Enable the unit that starts containers Docker skipped at boot
become: true
ansible.builtin.systemd:
name: docker-start-missed.service
enabled: true
daemon_reload: true
when: not ansible_check_mode
tags: [docker, configure]
- name: Take out the unit that starts containers Docker skipped
ansible.builtin.include_tasks: unit_absent.yml
vars:
__docker_unit_name: docker-start-missed
__docker_unit_script: /usr/local/sbin/docker-start-missed
when: not (docker_start_missed | bool)
tags: [docker, configure]
- name: Install the shutdown unit that ends containers while the network is up
when: docker_stop_at_shutdown | bool
tags: [docker, configure]
block:
- name: Install the script that ends containers at shutdown
become: true
ansible.builtin.copy:
src: docker-stop-at-shutdown
dest: /usr/local/sbin/docker-stop-at-shutdown
owner: root
group: root
mode: "0755"
tags: [docker, configure]
# Stop order is the reverse of start order: Before= makes docker stop first, so no restart policy can interfere, and After= the network
# and remote file systems makes this unit stop before them, while NFS can still be reached. The work is the stop action, which systemd
# runs only for a unit that is active: RemainAfterExit keeps it active after its start action, and the role starts it below.
- name: Install the unit that ends containers at shutdown
become: true
ansible.builtin.copy:
dest: /etc/systemd/system/docker-stop-at-shutdown.service
owner: root
group: root
mode: "0644"
content: |
[Unit]
Description=Stop containers while the network and NFS shares are still up
After=network-online.target remote-fs.target
Before={{ docker_service }}.service
ConditionPathExists=/usr/bin/docker
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/bin/true
Environment=DOCKER_STOP_TIMEOUT={{ docker_stop_at_shutdown_timeout }}
ExecStop=/usr/local/sbin/docker-stop-at-shutdown
TimeoutStopSec={{ (docker_stop_at_shutdown_timeout | int) + 30 }}
[Install]
WantedBy=multi-user.target
register: __docker_stop_at_shutdown_unit
tags: [docker, configure]
# Started now, so the first shutdown after this run is covered without a reboot. Not in check mode: the unit file above is only
# simulated there, and a dry run must not start anything. A changed unit is not restarted, because stopping it runs its stop action,
# which ends every container.
- name: Enable and start the unit that ends containers at shutdown
become: true
ansible.builtin.systemd:
name: docker-stop-at-shutdown.service
enabled: true
state: started
daemon_reload: true
when: not ansible_check_mode
tags: [docker, configure]
- name: Take out the shutdown unit that ends containers
ansible.builtin.include_tasks: unit_absent.yml
vars:
__docker_unit_name: docker-stop-at-shutdown
__docker_unit_script: /usr/local/sbin/docker-stop-at-shutdown
when: not (docker_stop_at_shutdown | bool)
tags: [docker, configure]
+10
View File
@@ -21,3 +21,13 @@
{% endif %}
quiet: true
tags: always
- name: Check the unit timings
ansible.builtin.assert:
that:
- docker_start_missed_settle_seconds | int(-1) >= 0
- docker_stop_at_shutdown_timeout | int(0) >= 1
fail_msg: >-
docker_start_missed_settle_seconds must be a whole number of at least 0 and docker_stop_at_shutdown_timeout a whole number of at least 1.
quiet: true
tags: always