feat(docker): manage /etc/docker/daemon.json (live-restore, log driver, extra options)
Build / build (pull_request) Successful in 51s
Molecule / molecule (pull_request) Canceled after 0s
Lint / lint (pull_request) Canceled after 1m20s

Adds docker_daemon_options, docker_live_restore, docker_log_driver,
docker_log_max_size, docker_log_max_file and docker_daemon_restart.

The file is written, before the packages are installed, only when one of
the first five is set, so a host that sets none is unchanged; the role
then owns the whole file. It restarts dockerd only when
docker_daemon_restart is true, because a restart with live-restore off
ends every container, and says so when the file applies later. A key set
by both docker_daemon_options and a dedicated input, and a bad log value,
stop the run before any change.

Closes #375

Assisted-by: Claude Code:claude-sonnet-5-5
This commit is contained in:
Dragonish committed 2026-11-25 19:42:06 +00:00
1 parent 428b88a822
commit 887302b208
10 files changed
+302 -1

No files matched your search

+18 -1
View File
@@ -35,13 +35,19 @@ The role targets Ubuntu hosts only: it installs from Docker's Ubuntu repository,
| `docker_start_missed_settle_seconds` | `30` | no | Seconds the boot unit waits after Docker answers, so dockerd's own restarts finish first. |
| `docker_stop_at_shutdown` | `false` | no | Install a unit that ends container processes at shutdown while the network and NFS are still up. |
| `docker_stop_at_shutdown_timeout` | `30` | no | Seconds the shutdown unit waits after SIGTERM before it sends SIGKILL (at least 1). |
| `docker_daemon_options` | `{}` | no | Extra keys merged into `/etc/docker/daemon.json` (see [Daemon settings](#daemon-settings)). |
| `docker_live_restore` | `false` | no | Write `"live-restore": true`. |
| `docker_log_driver` | empty | no | Written as `log-driver`, for example `local`. |
| `docker_log_max_size` | empty | no | Written as the `max-size` log option, for example `10m`. |
| `docker_log_max_file` | empty | no | Written as the `max-file` log option, a string, for example `"3"`. |
| `docker_daemon_restart` | `false` | no | Restart dockerd when `daemon.json` changed. |
| `docker_service` | `docker` | no | systemd unit name. |
| `docker_group` | `docker` | no | Group for `docker_users`. |
| `docker_users` | `[]` | no | Users added to the group. |
## Tags
`docker` (everything), `install`, `update`, `remove`, `configure` (group membership and the two units). The tasks in `remove.yml` carry `docker` and `remove`.
`docker` (everything), `install`, `update`, `remove`, `configure` (group membership, the daemon settings and the two units). The tasks in `remove.yml` carry `docker` and `remove`.
Update (`docker_update: true`) and removal (`docker_remove: true`) are opt-in: run them on a host that is safe to change.
@@ -60,6 +66,16 @@ Things to know before you switch `docker_stop_at_shutdown` on:
- The script reads the layout of the systemd cgroup driver on cgroup v2 (`/sys/fs/cgroup/system.slice/docker-<id>.scope`, and the cgroups below it). On a host that lays containers out differently it finds nothing, logs "no container scopes" and changes nothing, so check the journal (`journalctl -t docker-stop-at-shutdown -b -1`) after the first reboot.
- `docker-start-missed --dry-run` lists what the boot unit would start; add `--settle 0` to skip the wait.
## Daemon settings
With any of `docker_daemon_options`, `docker_live_restore`, `docker_log_driver`, `docker_log_max_size` or `docker_log_max_file` set, the role writes `/etc/docker/daemon.json` (root, mode `0644`, valid JSON) before it installs the packages, so a first start of dockerd already reads it. With none set, it writes nothing and leaves an existing file alone.
- **The role owns the whole file.** A key that is not in the inputs is removed, including one a person added by hand. Put every key you need in `docker_daemon_options`.
- **A key set twice is refused.** `live-restore`, `log-driver` and `log-opts` in `docker_daemon_options` stop the run when the dedicated input also sets them.
- **No restart by default.** A change applies at the next dockerd restart, and the role says so. A restart with `live-restore` off ends every container, so `docker_daemon_restart: true` is for a host where that is acceptable, or one that already runs with `live-restore` and whose change needs no more than a restart. Not every key can be applied by a reload; see the Docker documentation for the ones that can.
- **A bad value stops dockerd, not the role.** The role checks the shape of the log inputs, not whether the driver accepts the options: dockerd refuses to start with a rejected one (the `local` driver, for example, refuses some `max-file` and `compress` combinations). Try a new value on a host that can be down before it reaches the others.
- Setting an input back to its default does not remove a key from a file already written; `docker_remove` leaves the file in place.
## Example play
```yaml
@@ -81,6 +97,7 @@ What it shows, with the role's tasks running unchanged:
- The packages are installed from that repository, the unit is enabled and active, and `docker_users` join `docker_group`.
- `docker_remove` with `docker_remove_data` removes the source line, the keyring, the packages, the unit and `/var/lib/docker`.
- Both units are installed, enabled, ordered against `docker.service` as stated, the shutdown unit is active, a unit is removed when its input is `false`, and `docker_remove` removes both.
- With the daemon inputs set, `/etc/docker/daemon.json` is valid JSON with exactly the expected keys, mode `0644` and root-owned. A change with `docker_daemon_restart: false` leaves dockerd running (its start time is unchanged); with `true` it restarts it. A key set both in `docker_daemon_options` and by a dedicated input, and a bad log size, driver or file count, are refused before any change.
- The argument check refuses a mapping for `docker_users`, a non-boolean `docker_remove` and a `docker_stop_at_shutdown_timeout` of 0.
- A second run changes nothing (molecule's idempotence check).