How to stop and remove docker containers in a network

How to list docker containers in a network

Inspecting a docker network will list containers along with other information, we can use the --format flag to get a newline separated list.

docker network inspect <NAME> --format='{{range $id, $_ := .Containers}}{{println $id}}{{end}}

// Example output

Stopping and removing containers in a docker network

docker <stop|rm> $(docker network inspect <NAME> \
    --format='{{range $id, $_ := .Containers}}{{println $id}}{{end}}')

After much Googling I couldn't find how to do this anywhere, if there's a better way please let me know.

No Comments Yet