Request icon
OutputValues icon
Loop icon
Log icon
If icon
SlackIncomingWebhook icon
Pause icon
Commands icon
Docker icon
Fail icon
Schedule icon

Reclaim Abandoned Docker Networks Before the Address Pool Runs Out

Detect user-defined Docker networks with no containers attached, remove them behind an approval, then re-list the daemon and fail if any survived.

Categories
Infrastructure

Safety

Point this at a build host or a CI runner. Never point it at a daemon whose networks other workloads share. Never point it at a shared developer machine either, unless every network that matters is in protected_name_pattern first. A removal is not reversible. The subnet goes back to the pool. The DNS aliases go with it. A stopped container that was created with that network will not start again until the network is recreated under the same name.

The dangerous case is a shared network that is idle at the moment you run this. An empty window makes it look abandoned, so a network every workload on the host attaches to on a schedule is exactly what the grace window cannot see. Put its name in protected_name_pattern before the first run, not after. The default pattern covers common prefixes only, so a shared network named outside that set needs adding by hand.

Four separate guards decide what is removable. Each one on its own is enough to keep a network:

  • The Engine API type=custom filter, so bridge, host and none never enter the flow, plus a second check on those three names in the selection step.
  • A live inspect per network. Anything with one or more attached containers is kept.
  • protected_name_pattern, which wins over every other rule.
  • min_age_minutes, so a network a compose stack created seconds ago is left alone.

On top of those, nothing is removed unless auto_remediate is true, dry_run is false and a human resumes the Pause with approved: true. The ceiling in max_removals bounds a single run even then.

How it works

  1. list_networks (io.kestra.plugin.core.http.Request) reads GET /networks with the Engine API filters type=custom and the drivers in target_drivers. The daemon's own three networks are gone before the flow sees anything.
  2. candidates (io.kestra.plugin.core.output.OutputValues) reduces that to id and name. The list endpoint reports an empty Containers map for every network, including ones with a running container attached, so the list alone cannot tell idle from busy.
  3. inspect_each (io.kestra.plugin.core.flow.Loop) does one read-only GET /networks/{id} per candidate. The attached containers and the IPAM subnets exist only on the inspect response.
  4. window freezes the age cutoff once, so detection and verification share one instant.
  5. assess gives every network a verdict: KEEP_BUILTIN, KEEP_ATTACHED, KEEP_PROTECTED, KEEP_FRESH, KEEP_OVER_CAP or RECLAIM. It also computes the IPv4 address count behind each subnet, which is the share of the default address pool that comes back.
  6. summary logs the verdict counts, every target with its subnet, the total addresses at stake and which networks are held by which containers.
  7. route branches on whether anything is reclaimable. When it is, Slack gets the finding. The write path needs auto_remediate true plus dry_run false plus an approved Pause.
  8. remediate_remove runs one docker network rm <id> per target through io.kestra.plugin.scripts.shell.Commands on docker:27-cli, sequential, so a failure belongs to one network.
  9. relist reads GET /networks again with no driver filter, result joins the plan with the daemon as it is now. verify fails the run through io.kestra.plugin.core.execution.Fail if a target is still present or if a network the plan kept went missing.

docker network prune is deliberately not used. Neither is io.kestra.plugin.docker.cli.Prune with pruneType: NETWORKS. Prune takes no name allowlist, so a protected network on the same daemon is swept up with the rest. It reports no per-network result either, so there is nothing to verify a removal against. Per-target docker network rm gives one gated removal and one verifiable outcome each.

What you get

  • The reason unrelated builds start failing, named and measured: how many subnets leftover networks hold, how many addresses come back.
  • A detector that cannot confuse a busy network for an idle one, because it reads the inspect response rather than the list.
  • Read-only by default. Three gates plus a ceiling before anything is removed.
  • A re-read of the daemon that fails the run, so a refused removal cannot report success.
  • A collateral check: every network the plan kept has to still be there at the end.

Who it's for

  • Platform engineers running self-hosted CI runners, where compose-based jobs leak a network per build.
  • Teams that have already hit all predefined address pools have been fully subnetted and cleared it by hand.
  • Anyone who wants the Docker network cleanup a cron job cannot do safely, with a name allowlist, an approval and proof the removal landed.

Why orchestrate this with Kestra

A cron job running docker network prune cannot tell a protected network from an abandoned one, cannot wait for a human, cannot report which subnets it freed, cannot tell you it failed. It either removes everything unused or nothing. Kestra reads the daemon, inspects each network individually, computes the plan in one expression, holds the change behind an approval, removes one network at a time so a failure is attributable, reads the daemon again to prove what landed, and keeps the history of every run against every host.

Tested end to end

On Kestra 2.0.5 OSS against an isolated Docker 27.5.1 daemon (Docker-in-Docker, its own Engine API on tcp://2375), with a local HTTP sink standing in for Slack. The daemon held three built-in networks plus seven custom bridges: leaked-ci-1, -2 and -3 (empty, aged), app-stack-a and app-stack-b (one running container each), prod-data (empty, aged, matching the default pattern) plus kw7 (empty, aged, a shared network whose name does not match the default pattern, so it was added to protected_name_pattern by hand). Every number below was cross-checked with docker network ls plus docker network inspect against that daemon, never with the Kestra log alone. The runs used min_age_minutes: 1 so the grace window could be exercised inside one session.

Run Inputs Result
1 defaults, a container on every leaked network No target. KEEP_ATTACHED 5, KEEP_PROTECTED 2, nothing reclaimable
2 identical, the three holder containers removed 3 targets named with their subnets, 196608 addresses, RECLAIM 3. Nothing removed
3 auto_remediate: true, dry_run: true Same 3 named. docker network ls 10 before, 10 after
4 auto_remediate: true, dry_run: false, pause resumed approved: false approval_declined. 10 before, 10 after. No removal task ran at all
5 auto_remediate: true, dry_run: false, pause resumed approved: true 3 removed, 196608 addresses across 3 subnets freed, docker network ls 10 before, 7 after. The 2 attached plus the 2 protected verified still present
6 a network created seconds earlier, max_removals: 2 The new one spared as KEEP_FRESH, the third eligible held as KEEP_OVER_CAP, 2 still selected
7 protected_name_pattern narrowed to one name prod-data flipped from KEEP_PROTECTED to RECLAIM, the one protected name stayed kept
8 a container attached to one target during the pause, plus a kept network removed out of band docker network rm reported success on the refused one, verify failed the run through Fail naming the survivor plus the collateral, the Slack errors block posted

Run 5 is the headline. Creating one network straight after it handed that network 172.21.0.0/16, the exact subnet leaked-ci-1 had been holding, so the address space really did come back to the pool rather than only disappearing from a list.

Run 8 is the case the re-verify exists for. The daemon answered network leaked-ci-2 ... has active endpoints and refused, the removal task still went green, then the re-read caught it. A flow that trusted the removal task would have reported a clean reclaim.

The detector was proved able to fail on the same inputs. With a container on each leaked network it reported 0 targets. With those containers removed, no input changed, it reported 3.

Prerequisites

Secrets

  • SLACK_WEBHOOK_URL: the incoming webhook the findings notice and the errors block post to.

Inputs

Input Default What it does
docker_host tcp://docker-host:2375 TCP endpoint of the daemon to clean
docker_cli_network bridge Network the docker CLI container joins
target_drivers [bridge] Drivers to consider
protected_name_pattern ^(prod|shared|ingress|kestra|monitoring)([-_.].*)?$ Never removed, whatever else is true
min_age_minutes 60 Grace window before an empty network is a target
max_removals 10 Ceiling per run
auto_remediate false Must be true for any removal
dry_run true Must be false for any removal

The Engine API has no bulk network inspect call, so the reads go over HTTP, which means a TCP endpoint or a read-only socket proxy rather than a bare Unix socket. Keep it off any untrusted network: an open Docker API is root on that host. The one-off docker:27-cli container must be able to reach docker_host, so leave docker_cli_network as bridge for a daemon that resolves by DNS. Point it at the user-defined network when the daemon is itself a container.

Quick start

  1. Add the SLACK_WEBHOOK_URL secret.
  2. Import the flow, set docker_host and docker_cli_network for one build host, then run it with the defaults. It only reports.
  3. Read the summary. Put anything in KEEP_ATTACHED you did not expect, plus any network a service attaches to on a schedule, into protected_name_pattern.
  4. Re-run with auto_remediate: true and dry_run: false, read the plan on the paused execution, then resume with approved: true.
  5. Enable the Schedule trigger once the pattern is right. It stays in safe mode until you change its inputs.

How to extend

  • Filter on labels as well as names. Compose stamps com.docker.compose.project on the networks it creates, so a label check separates a leaked CI network from one somebody made by hand.
  • Write the plan to a KV pair per host and alert when the same network shows up idle on two consecutive runs, so a network with a weekly workload is never a target.
  • Add io.kestra.plugin.docker.cli.Prune with pruneType: VOLUMES behind the same gate, for the anonymous volumes the same jobs leave behind.
  • Loop the whole flow over a list of hosts with a subflow, so one execution covers a fleet.
  • Read docker info for the configured default-address-pools and alert on the share consumed rather than on idle networks alone.

Links

This blueprint was created by zkasuran.

See How

New to Kestra?

Use blueprints to kickstart your first workflows.