Sub topic 8
Removing unused networks
This chapter assumes you can run Docker commands, know the difference between an image and a container, and have seen docker network ls output. Networks use almost no disk space. The reason to remove them is tidiness and avoiding exhausted address pools, and you also need to know what docker system prune does to them.
What a Docker network is
A Docker network is a virtual switch that connects containers. Docker creates one for each Compose project and for each docker network create call. A network is unused when no container, running or stopped, is attached to it.
Every network that Docker creates takes a subnet from a limited address pool. After many projects have been created and abandoned, the pool can run out. Docker then fails with an error such as all predefined address pools have been fully subnetted. Removing old networks fixes this.
List the networks first
docker network lsThree networks always exist: bridge, host, and none. Docker never removes them.
Prune unused networks
docker network pruneDocker lists nothing in advance. It asks Are you sure you want to continue? [y/N] and then deletes every network that has no attached containers. Add -f to skip the question.
docker network prune -fDocker also skips a network that was created less than a moment ago and has no containers yet. It is simplest to treat any empty network as a candidate, including one you created for a project you have not started.
Preview what will be removed
docker network prune has no dry-run flag. To see the likely targets, list the networks and check which have containers:
docker network ls
docker network inspect my-net --format '{{len .Containers}}'The second command prints the number of attached containers. A result of 0 means the network is a prune candidate.
Filter by age or label
Network prune accepts the same --filter option as the other prune commands. Use until to keep recent networks:
docker network prune --filter "until=168h"This removes only unused networks created more than seven days ago. Use label to target networks by a label you set yourself:
docker network create --label env=temp scratch-net
docker network prune --filter "label=env=temp"Putting label!=keep in the filter does the opposite and protects the networks that carry that label.
How it fits with the system prune
docker system prune includes network pruning. Its steps are shown below.
You rarely need docker network prune by itself if you already run the system prune. Use it alone when you want to clear networks without touching images or cache.
Common errors
- A network cannot be removed with
docker network rm. A container, even a stopped one, is still attached. Remove the container first withdocker container pruneordocker rm, then remove the network. - A Compose project network keeps reappearing.
docker compose upcreates it again. Rundocker compose downto remove it with the project. - Prune says nothing was deleted. Every remaining network has a container attached.
Exercise
You created a network with docker network create db-net and have not started any container on it yet. You run docker network prune -f. What happens, and how could you prevent it?
Show answer
The network is deleted because it has no attached containers, so the next docker run --network db-net fails. You can prevent this by giving the network a label such as keep and running docker network prune --filter "label!=keep" -f, or by defining the network in a Compose file so it is created each time.
Exercise
Which command shows how many containers are attached to a network named app-net, and which result marks it as a prune candidate?
Show answer
docker network inspect app-net --format '{{len .Containers}}'. A result of 0 means no containers are attached, so docker network prune would remove it.