feat(container-prune): prune unused images weekly #61

Merged
claudiomaradonna merged 3 commits from feat/container-prune into main 2026-12-02 23:27:33 +01:00
Collaborator

Why

Hosts that run containers with Docker or Podman keep every image they
ever pulled or built, and Docker also keeps its build cache. After a few
days of deploys this takes a lot of disk, and nothing in castrum cleans
it up: today someone has to log in and run prune by hand.

What changed

A new service role, container-prune, switched on per host with
container_prune_enabled. It installs container-prune.service and a
weekly container-prune.timer (persistent, with a random delay of up to
an hour). Where docker_engine_enabled is set, the service runs
docker image prune -a and docker builder prune. Where
podman_enabled is set, it runs podman image prune -a on root's
storage. Each command gets --force --filter until=168h. Volumes,
containers and networks are never pruned. The age, the calendar and the
delay are role variables.

A host with the flag on and neither engine flag fails the run at this
role, before any unit is written, also under --check. The role honours
update_only. The loader includes it after docker-engine.

The role's README explains:

  • what is removed and what is kept;
  • that until reads an image's creation time, not its last use;
  • re-pulls on swarm nodes and on stacks taken down;
  • how to read the timer back, or remove it by hand.

How to verify

Prerequisites: the Setup toolchain, and
rootless podman for molecule. Base main (d793429); branch
feat/container-prune.

  • ./tests/molecule/run.sh roles/container-prune builds a Debian 12
    container running systemd, with stub docker and podman that log
    their arguments. It applies the role twice and checks that the second
    run changes nothing. It then checks that the timer is enabled and
    active, starts the service, and compares the logged commands with the
    three expected ones. Run on the branch: exit 0, no ^ERROR line. On
    main it fails, because the role does not exist.
  • ./tests/gates/check_container_prune.sh covers three cases. With the
    flag on and no engine flag, the run fails before the units, also
    under --check, naming the flags. With the flag off, or under
    update_only=true, no task runs. Passes on the branch. It fails on
    the first commit, which has no refusal ("the failure does not name the
    flags"), and on main.
  • make check passes on every commit of the branch, all gates included.
    The molecule scenario above is the only part of make test that this
    change touches.

Considerations

  • until reads creation time, so it mostly protects images built on
    the host. An image of a compose stack taken down, or of a swarm
    service scaled to zero or running on other nodes, is removed. It is
    pulled again when next needed, which needs the registry and, on
    swarm, its credentials. The README says to leave the flag off where
    the registry cannot be relied on.
  • Rootless Podman storage is out of scope. Only
    /var/lib/containers is pruned.
  • The role also runs on hosts whose docker-engine is in assert mode,
    because it does not touch the engine's configuration or packages.
  • Switching the flag off leaves the timer in place, as with every
    flag-gated role. The README gives the removal commands.
  • docker builder prune runs without --all, so only dangling build
    cache is removed.
  • Review findings set aside:
    • The verify step no longer checks the calendar. systemd reports
      weekly in its normalized form, and the goal asks only for an
      enabled, active timer.
    • The gate has no override for the unit directory. The refusal keeps
      the controller's /etc untouched, and an override would add a
      variable.
## Why Hosts that run containers with Docker or Podman keep every image they ever pulled or built, and Docker also keeps its build cache. After a few days of deploys this takes a lot of disk, and nothing in castrum cleans it up: today someone has to log in and run `prune` by hand. ## What changed A new service role, `container-prune`, switched on per host with `container_prune_enabled`. It installs `container-prune.service` and a weekly `container-prune.timer` (persistent, with a random delay of up to an hour). Where `docker_engine_enabled` is set, the service runs `docker image prune -a` and `docker builder prune`. Where `podman_enabled` is set, it runs `podman image prune -a` on root's storage. Each command gets `--force --filter until=168h`. Volumes, containers and networks are never pruned. The age, the calendar and the delay are role variables. A host with the flag on and neither engine flag fails the run at this role, before any unit is written, also under `--check`. The role honours `update_only`. The loader includes it after `docker-engine`. The role's README explains: - what is removed and what is kept; - that `until` reads an image's creation time, not its last use; - re-pulls on swarm nodes and on stacks taken down; - how to read the timer back, or remove it by hand. ## How to verify Prerequisites: the [Setup](CONTRIBUTING.md#setup) toolchain, and rootless podman for molecule. Base `main` (d793429); branch `feat/container-prune`. - `./tests/molecule/run.sh roles/container-prune` builds a Debian 12 container running systemd, with stub `docker` and `podman` that log their arguments. It applies the role twice and checks that the second run changes nothing. It then checks that the timer is enabled and active, starts the service, and compares the logged commands with the three expected ones. Run on the branch: exit 0, no `^ERROR` line. On `main` it fails, because the role does not exist. - `./tests/gates/check_container_prune.sh` covers three cases. With the flag on and no engine flag, the run fails before the units, also under `--check`, naming the flags. With the flag off, or under `update_only=true`, no task runs. Passes on the branch. It fails on the first commit, which has no refusal ("the failure does not name the flags"), and on `main`. - `make check` passes on every commit of the branch, all gates included. The molecule scenario above is the only part of `make test` that this change touches. ## Considerations - `until` reads creation time, so it mostly protects images built on the host. An image of a compose stack taken down, or of a swarm service scaled to zero or running on other nodes, is removed. It is pulled again when next needed, which needs the registry and, on swarm, its credentials. The README says to leave the flag off where the registry cannot be relied on. - Rootless Podman storage is out of scope. Only `/var/lib/containers` is pruned. - The role also runs on hosts whose `docker-engine` is in `assert` mode, because it does not touch the engine's configuration or packages. - Switching the flag off leaves the timer in place, as with every flag-gated role. The README gives the removal commands. - `docker builder prune` runs without `--all`, so only dangling build cache is removed. - Review findings set aside: - The verify step no longer checks the calendar. systemd reports `weekly` in its normalized form, and the goal asks only for an enabled, active timer. - The gate has no override for the unit directory. The refusal keeps the controller's `/etc` untouched, and an override would add a variable.
Hosts running Docker or Podman keep every image they pulled or built,
and the store grows by gigabytes within days. A weekly systemd timer
removes images, and Docker's build cache, that no container uses and
that are older than container_prune_age; volumes, containers and
networks stay. Only root's Podman storage is pruned.
A service with no engine flag would fire every week and prune nothing,
hiding the missing flag. The role now fails first, also under --check,
and the container_prune gate holds that and the flag-off and
update_only paths.
docs(container-prune): document the role and its gate
Some checks failed
check / check (push) Has been cancelled
check / check (pull_request) Successful in 25m1s
a5c49f5a60
The README answers what an operator weighs before switching the flag
on: what the weekly prune removes and keeps, that until reads an
image's creation time, the re-pulls a swarm node or a stack taken down
may need, and how to read the timer back or remove it.
claudiomaradonna deleted branch feat/container-prune 2026-12-02 23:27:39 +01:00
First-time contributor

Command failed

The agent did not complete this command, so nothing came of it. A person should look at this thread.

The run failed at step "aoe_purge". The operator reads the job logs in the Windmill editor, or finds the run with: wmill job list --path f/agents/bot_commands

## Command failed The agent did not complete this command, so nothing came of it. A person should look at this thread. The run failed at step "aoe_purge". The operator reads the job logs in the Windmill editor, or finds the run with: `wmill job list --path f/agents/bot_commands`
Sign in to join this conversation.
No description provided.