Files
Kamino/roles/docker/README.md
T
Dragonish 887302b208
Build / build (pull_request) Successful in 51s
Molecule / molecule (pull_request) Canceled after 0s
Lint / lint (pull_request) Canceled after 1m20s
feat(docker): manage /etc/docker/daemon.json (live-restore, log driver, extra options)
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
2026-11-25 19:42:06 +00:00

116 lines
12 KiB
Markdown

# docker
Installs Docker Engine from the upstream Docker APT repository, optionally updates it, or removes it, and adds listed users to the `docker` group. Variables and their types are defined in [`meta/argument_specs.yml`](meta/argument_specs.yml), which is the source of truth.
## Supported platforms
Ubuntu 24.04 (noble), the platform the molecule scenario runs on. The key and repository URLs point at `download.docker.com/linux/ubuntu`, and the source line uses `ansible_distribution_release`. The apt architecture is derived from the host (`x86_64` gives `amd64`, `aarch64` gives `arm64`); any other architecture fails the play until `docker_apt_arch` is set. A limited molecule scenario covers the role's own logic (see [Tests](#tests)); the real Docker Engine is not tested.
Untested platforms: Ubuntu releases other than 24.04; `meta/main.yml` declares Ubuntu `all`.
The role targets Ubuntu hosts only: it installs from Docker's Ubuntu repository, so it does not cover Debian trixie (Proxmox hosts).
## Requirements and run order
- Needs gathered facts and privilege escalation.
- Use it by its fully qualified name, `mortis.kamino.docker`.
- Run it before any role that needs the Docker group. Roles that install later and create their own user must add that user to the group themselves (see `docker_users`).
- Role dependencies: none.
- The `ufw` role is not a dependency and is not called: Docker publishes ports through iptables and owns no UFW rule.
## Role variables
| Variable | Default | Required | Description |
|---|---|---|---|
| `docker_remove` | `false` | no | Remove Docker instead of installing it. |
| `docker_remove_data` | `false` | no | With `docker_remove`, purge packages and delete `/var/lib/docker`. |
| `docker_update` | `false` | no | Upgrade packages to their current candidate version. |
| `docker_packages` | `docker-ce`, `docker-ce-cli`, `containerd.io` | no | Packages managed. |
| `docker_keyring_path` | `/etc/apt/keyrings/docker.asc` | no | Repository key location. |
| `docker_apt_list_path` | `/etc/apt/sources.list.d/docker.list` | no | APT source file. |
| `docker_gpg_url` | `https://download.docker.com/linux/ubuntu/gpg` | no | Key URL. |
| `docker_repo_base_url` | `https://download.docker.com/linux/ubuntu` | no | Repository base URL. |
| `docker_apt_arch` | derived from the host | no | Architecture in the APT source. Empty derives it (`x86_64` gives `amd64`, `aarch64` gives `arm64`); another architecture fails until this is set. A value set here is always used; lowercase letters and digits only. |
| `docker_start_missed` | `false` | no | Install a boot unit that starts the containers Docker skipped after a reboot (see [Boot and shutdown units](#boot-and-shutdown-units)). |
| `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, 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.
## Boot and shutdown units
Both are opt-in, for a host that runs Docker with `live-restore` (a `docker.service` restart then leaves containers running). They are part of this role, so they exist only on a host you apply it to, and each does nothing when `/usr/bin/docker` does not exist.
- **`docker_start_missed`**: a reboot of the whole host kills the containers, and dockerd's restore pass at the next boot sometimes skips one without trying (`RestartCount` stays 0; Docker reports exit code 255). The unit `docker-start-missed.service` runs once per boot after Docker, waits `docker_start_missed_settle_seconds`, and starts a container only if its restart policy is `unless-stopped` or `always`, it exited with code 255, and it finished after this boot began. A container stopped on purpose (exit code 0, 137 or 143) stays stopped. It is `Type=simple`, so it never delays or fails the boot.
- **`docker_stop_at_shutdown`**: with `live-restore` dockerd does not stop containers at host shutdown, and systemd-shutdown signals them after the network is gone. A container that holds an NFS mount then blocks in the kernel's final unmount until the SIGKILL timer. The unit `docker-stop-at-shutdown.service` is started by the role; its stop action runs after Docker has stopped and before the network and remote file systems, sends SIGTERM to every container process, waits up to `docker_stop_at_shutdown_timeout` seconds, then SIGKILL, and logs to the journal under the tag `docker-stop-at-shutdown`. It never calls `docker stop` or `docker kill`, which would set the manually-stopped marker, so a container with `unless-stopped` starts again at the next boot. Check that `systemctl is-active docker-stop-at-shutdown` says `active`, because the stop action runs only for an active unit.
Setting an input back to `false` removes that unit's file and script. The role never stops a running shutdown unit, because stopping it runs its stop action and ends every container; it takes effect as systemd reloads and reboots.
Things to know before you switch `docker_stop_at_shutdown` on:
- The unit is ordered before `docker.service` and after `network-online.target` and `remote-fs.target`. That ordering applies at boot as well: Docker starts only after the network and the remote file systems are up, so an unreachable NFS server delays Docker until its mount gives up.
- 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
- hosts: standalone_vms
roles:
- role: mortis.kamino.docker
vars:
docker_users: [deploy]
```
## Tests
`molecule/default` runs in CI only (`.gitea/workflows/molecule.yml`, no local runner), on one platform: Ubuntu 24.04 (noble), the `molecule-ubuntu2404` test container (`${MOLECULE_IMAGE}`). A real Docker Engine cannot run there: `dockerd` needs the bridge, iptables rules and a delegated cgroup, and the container has no privileged mode. So the scenario builds a local APT repository, signs it with a throwaway key, and points the role at it with `docker_gpg_url` and `docker_repo_base_url`. The repository holds empty stand-ins for `docker-ce`, `docker-ce-cli` and `containerd.io`. The stand-in `docker-ce` ships a stub `docker.service` (`sleep infinity`) and creates the `docker` group, as the real package does.
What it shows, with the role's tasks running unchanged:
- The key is fetched, stored at `docker_keyring_path` with mode `0644`, and apt really verifies the repository against it (an unsigned or wrongly signed repository would leave no install candidate).
- The source line has the architecture, the `signed-by` keyring and `ansible_distribution_release`.
- 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).
What it does not show: what the units do at a real boot or shutdown (the container has no dockerd, no NFS shares and no reboot; the selection logic of `docker-start-missed` is unit-tested in `tests/scripts/test_docker_files.py`), that the upstream repository and key at `download.docker.com` are reachable and valid, that the real packages install, that `dockerd` starts, anything about container networking or iptables, the `docker_update` path (`tasks/update.yml`), and removal without `docker_remove_data` (the removal stops the unit first, so it can run only once per install). It needs the Ubuntu mirrors, for the `curl` the role installs. These gaps are a known residual risk of the role.
## Notes
- Idempotent: yes.
- `docker_remove` is checked first: when true, only `remove.yml` runs.
- Removal with `docker_remove_data` is destructive. It deletes `/var/lib/docker`.
- The update task resolves each package's candidate with `apt-cache policy` and fails if a package has none. It installs `package=version`, never `latest`.
- Installing or updating the packages notifies `Restart docker`. The `meta: flush_handlers` at the end of `tasks/main.yml` is untagged, so it runs only without a `--tags` selection; under `--tags update` (or `install`) it is skipped and the handler runs at the end of the play instead.
- Do not combine with anything that installs the distro `docker.io` package on the same host: this role installs `docker-ce` from Docker's repository, and the two packages conflict.
- Docker bypasses UFW for published ports (`-p`), so UFW rules do not restrict container ports.
- `no_log` is not used: the role handles no secrets.