brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: docker network prune --filter label!= spares the networks you mark

Docker
~/tips/tip-cleanup-docker-network-prune-label

docker network prune

Removes unused Docker networks, optionally only old ones or only ones without a label.

Unused Docker networks pile up quietly. Test stacks come and go, and each one leaves a network behind. docker network prune clears them, and with a label I can mark the few I want to keep.

docker network prune

It removes all unused networks. The docs define unused as not referenced by any container, and the built-in networks bridge, host and none are never pruned. Docker asks for confirmation first and warns that it will remove all custom networks not used by at least one container. -f skips the question.

Two filters narrow it down. until takes a timestamp or a duration like 24h:

docker network prune --filter "until=24h"

The label filter is the one I like for a shared setup. Give the networks you want to keep a label when you create them:

docker network create --label keep homelab
docker network prune --filter "label!=keep"

The label!= form removes only networks that do not have that label. The plain label=key form does the opposite and removes networks that have it.

When you pass more than one filter, different keys are combined with AND, and the same key repeated is combined with OR. So --filter "label=foo" --filter "until=24h" only prunes networks that have the label and are older than a day.

The caveat is the definition of unused. A network that no container uses right now counts, even if you plan to attach something to it tomorrow. That includes networks you created by hand for later. Labelling them first is cheap, and a prune without it is hard to undo.

Further reading: docker network prune

More Docker tips ONBUILD RUN /usr/local/bin/python-build --dir /app/src SHELL ["/bin/bash", "-c"] docker commit <container> myname/testimage:version3 docker import /path/to/exampleimage.tgz docker compose events All Docker tips (192) All topics

Link