Skip to content

fix(cli): devops docker prune removes stopped containers, unused networks and dangling images instead of failing on every engine #1116

Closed

Description

@dan-petty

Problem

Lines cited at dcafadf. No cited line has moved at 132ecc5. Paths are under src/devops_cli/ unless they start with tests/ or docs/. Paths marked docker-py are in the locked docker 7.2.0 (pyproject.toml:24).

devops docker prune fails on every engine, Docker and Podman alike:

  • After the confirmation, it calls client.system.prune(volumes=volumes) (commands/docker.py:200-201).
  • docker-py 7.2.0's DockerClient has no system attribute. Checked in the locked environment, its public attributes are configs, containers, df, events, from_context, from_env, images, info, login, networks, nodes, ping, plugins, secrets, services, swarm, version and volumes.
  • So the command ends in an AttributeError traceback before it sends any request. The call dates from v0.1.8.

The tests do not catch this:

  • The client in both prune tests is a bare MagicMock, which accepts any attribute (tests/test_docker.py:34, :42, :210, :218).
  • One test pins a tuple return value (:218), and the command parses that tuple (commands/docker.py:202-203). No docker-py prune call returns a tuple.

The docs describe a different command:

  • The cheatsheet maps it to docker system prune -af --volumes and says it deletes "dangling images and cache" (docs/cheatsheets/docker.md:24).
  • The knowledge base shows devops docker prune --all, a flag the command does not have, and says it cleans the build cache (ai/knowledge_base/it_domains/tools/docker.md:25-26).

docker-py already has the calls:

  • Each collection has prune(filters=None) (docker-py models/containers.py:1026, models/images.py:489, models/networks.py:216, models/volumes.py:97).
  • Each posts to the Engine API's /containers/prune, /images/prune, /networks/prune or /volumes/prune, and returns the reply as a dict.
  • The container, image and volume replies carry SpaceReclaimed. The network reply lists only the deleted networks.

Acceptance criteria

  • Prune calls.
    • devops docker prune --force calls containers.prune(), networks.prune() and images.prune(), once each, in that order and with no filters.
    • With --volumes, it then also calls volumes.prune(). Without --volumes, it does not.
    • No request is sent before the confirmation; --dry-run still sends none.
  • Reclaimed space.
    • The success line (MESSAGES.docker.pruned_success) reports the sum of the SpaceReclaimed values. A missing or null value counts as 0.
    • Example: containers 5 MiB, images 5 MiB and a network reply without the key report "10 MB". With --volumes and 1 MiB from volumes, the report is "11 MB".
    • The tuple branch (commands/docker.py:202-203) is removed.
  • Errors. A daemon error from any prune call (a mocked docker.errors.APIError) ends the command with exit 1 and one error line that holds the daemon's text, with no traceback.
  • Specced test clients.
    • A docker_client fixture in tests/conftest.py returns create_autospec(docker.DockerClient, instance=True).
    • Its containers, images, networks and volumes are create_autospec of docker-py's ContainerCollection, ImageCollection, NetworkCollection and VolumeCollection (instance=True). Its api is create_autospec(docker.APIClient, instance=True).
    • This matters because, checked in the locked environment, create_autospec of the client alone leaves those properties as plain MagicMocks and has no api at all, since api is set in __init__.
    • A test pins the fixture: client.system raises AttributeError, and client.containers.prune(volumes=True) raises TypeError.
    • Every Docker client mock in tests/test_docker.py uses the fixture, and the new prune tests do too.
  • No old call is left. rg -n 'system\.prune' src tests prints nothing.
  • Help and docs.
    • The command's help, its confirmation prompt and the --volumes help say what is removed: stopped containers, unused networks, dangling images and, with --volumes, unused volumes.
    • docs/commands/docker.md is generated from that help text and is not edited by hand. It is regenerated with devops docs generate, and the docs check passes.
    • The cheatsheet row maps the command to docker system prune [--volumes]. It says the build cache is cleared by devops docker cache --prune.
    • The knowledge-base lines show devops docker prune without --all and no longer mention the build cache.
  • Release.
    • Tests are offline and never build a real DockerClient. The new tests take well under 1 s together, and uv run devops ci passes.
  • Pending a person: on the devcontainer's Docker engine, and on rootless Podman reached through DOCKER_HOST, run devops docker prune --force and devops docker prune --force --volumes. Before each run, create a stopped container, a dangling image, an unused network, an unused anonymous volume and an unused named volume. Record in the task file what was removed, whether the named volume and tagged images were kept, and the reported space.

Key questions

  • Dangling images or all unused images? Proposed: dangling only.
    • images.prune() with no filter prunes only dangling images, as docker system prune without -a does.
    • So a prune never deletes a tagged base image that the next build needs.
    • No --all flag is added.
  • What does --volumes remove? Proposed: whatever volumes.prune() with no filter removes.
    • On Docker (Engine API 1.42 and later) that is unused anonymous volumes only, as docker system prune --volumes removes.
    • Podman's behaviour is recorded by the person step. The help says "unused volumes", which is true on both engines whichever volumes Podman removes.
    • No all filter is sent.
  • Does the prune also clear the build cache, as docker system prune does? Proposed: no.
  • Where does the prune live? Proposed: in a DockerEngineService method next to prune_build_cache.
    • It wraps daemon errors in DockerEngineError in the same way and returns the bytes reclaimed.
    • The command prints the error line and exits 1.

Strategic alignment

Activity

  1. added
    type/bug
    scope/cli
    priority/p2-medium
    on Oct 3, 2026
  2. dan-petty commented on Oct 8, 2026

    @dan-petty
    OwnerAuthor

    Linked: #1316, #1108, #1117 and #1327. devops docker prune calls client.system.prune (commands/docker.py:201), which the SDK lacks; #1316's Protocol must gain the prune calls, and #1327 owns tests/test_docker.py. Found by the 2026-10-08 backlog review.

  3. added a commit that references this issue on Oct 24, 2026
    adc122b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    priority/p2-mediumscope/clitype/bug

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions