Sub topic 8
Removing unused networks
This chapter covers how Docker networks use resources, how to find and remove the ones nothing uses, and which networks Docker will not let you remove. It assumes you can run docker run, docker ps, and docker network ls, and that you know an image is different from a container.
Why networks matter for cleanup
Networks take almost no disk space. A network is a small record in Docker's metadata plus some kernel objects, such as a bridge device and iptables rules. Removing unused networks will not free gigabytes.
Cleaning them up still helps, for these reasons:
- Each user-defined bridge network reserves an IP address range (a subnet). Docker's default address pools are finite. A host that creates many networks can fail with
could not find an available, non-overlapping IPv4 address pool among the defined range of networks. - Each bridge network adds a network interface to the host. Hundreds of them make
ip addranddocker network lshard to read. - Leftover networks can clash with a VPN or corporate subnet that uses the same address range.
Note that docker system df does not report networks. You will not see them in its output, so you have to inspect them with docker network ls.
Listing networks
docker network lsA fresh Docker host shows three networks:
NETWORK ID NAME DRIVER SCOPE
a1b2c3d4e5f6 bridge bridge local
b2c3d4e5f6a1 host host local
c3d4e5f6a1b2 none null localThese are built-in networks. Docker always keeps them, and you cannot remove them. Any other row was created by you, by Docker Compose, or by a tool that talks to the Docker API.
What counts as unused
docker network prune removes networks that have no containers attached. The check looks at attached containers, and it counts stopped containers too. A network is in use if any container, running or stopped, is connected to it. This has an effect you should plan for. If you have not removed your stopped containers, their networks stay. Clean containers first (see the chapter on removing stopped containers), then prune networks.
flowchart TDThe block above is only a placeholder for the order of operations. The real diagram follows.
Pruning unused networks
docker network pruneDocker prints a warning and asks for confirmation:
WARNING! This will remove all custom networks not used by at least one container.
Are you sure you want to continue? [y/N]Answer y to continue. The command prints the name of each removed network. Add -f (or --force) to skip the prompt, which is useful in scripts.
Previewing before you delete
network prune has no dry-run flag. To preview, list networks and check which have containers attached.
docker network ls --filter dangling=trueThe dangling filter on docker network ls is supported by current Docker releases and shows networks with no attached containers. If your version rejects it, inspect each custom network instead:
docker network inspect my-net --format '{{len .Containers}}'This prints the number of containers attached to my-net. A result of 0 means a prune would remove it.
Filtering by age and label
Like other prune commands, network prune accepts --filter.
docker network prune --filter "until=24h"This removes only unused networks created more than 24 hours ago. It protects networks you made a moment ago and have not yet attached anything to.
Labels let you target or protect specific networks. Create a network with a label:
docker network create --label keep=true shared-servicesThen exclude it from pruning:
docker network prune --filter "label!=keep=true"You can also prune only networks that carry a label, for example --filter "label=env=ci". Networks created by Docker Compose carry labels such as com.docker.compose.project, which lets you clean up one project's leftovers without touching others.
| Filter | Effect |
|---|---|
| `until=24h` | Only networks created more than 24 hours ago |
| `label=env=ci` | Only networks with that label |
| `label!=keep=true` | Skip networks with that label |
Networks that cannot be removed
Some removals fail even when you target them directly with docker network rm.
- Built-in networks.
bridge,host, andnoneare protected. Docker returns an error such asbridge is a pre-defined network and cannot be removed. - Networks with active endpoints. If a container is attached, Docker answers
network my-net has active endpoints. Disconnect or remove the container first. - Networks in use by a Swarm service. Overlay networks used by a service are not pruned until the service is removed.
docker network prune skips these cases silently. It only removes networks that are safe to remove.
A worked example
This sequence shows the dependency between containers and networks.
docker network create demo-net
docker run -d --name demo-web --network demo-net nginx:alpine
docker network prune -fThe prune removes nothing from this setup, because demo-web is attached to demo-net. Now stop the container and try again:
docker stop demo-web
docker network prune -fThe network is still there. A stopped container still counts as attached. Remove the container, then prune:
docker rm demo-web
docker network prune -fThe output now lists demo-net as deleted. This is the behavior that surprises people most often.
Common errors
- "network has active endpoints." A container is still attached. Run
docker ps -a --filter network=my-netto find it. - "Pool overlaps with other one on this address space." Two networks use the same subnet. Remove the unused one or set an explicit
--subneton the new one. - Prune seems to do nothing. Stopped containers are probably holding the networks. Prune containers first.
- A network you wanted to keep is gone. If you create a network before attaching containers, a prune can remove it in between. Use a
labelto protect it, or use theuntilfilter.
Where this fits
docker system prune includes network prune in its work. You rarely need the separate command unless you want to clear networks alone, apply network-specific filters, or free up address pools without touching images and volumes. The next chapters cover the filters in more detail and the combined command.
Exercise
You run docker stop web on the only container attached to the custom network app-net, and then run docker network prune -f. Does app-net get removed? What must you do for the next prune to remove it, and how can you check beforehand how many containers are attached?
Show answer
No. A stopped container still counts as attached, so app-net is kept. Remove the container with docker rm web and then run docker network prune -f. To check beforehand, run docker network inspect app-net --format '{{len .Containers}}'. Note that this count reflects containers Docker lists on the network, so use docker ps -a --filter network=app-net to confirm that no stopped containers remain.
Exercise
Write a command that removes unused networks older than 48 hours but never removes a network labeled keep=true.
Show answer
docker network prune -f --filter "until=48h" --filter "label!=keep=true"