Docker disconnects all containers from the docker network

I have a docker network "my_network". I want to remove this docker network using docker network rm my_network. Before that, I have to disconnect all my containers from this network. I can use docker network inspectand get output like

[
    {
        "Name": "my_network",
        "Id": "aaaaaa",
        "Scope": "some_value",
        "Driver": "another_value",
        "EnableIPv6": bool_value,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "10.0.0.0/1"
                }
            ]
        },
        "Internal": false,
        "Containers": {
            "bbb": {
                "Name": "my_container_1",
                "EndpointID": "ENDPOITID1",
                "MacAddress": "MacAddress1",
                "IPv4Address": "0.0.0.0/1",
                "IPv6Address": ""
            },
            "ccc": {
                "Name": "my_container_2",
                "EndpointID": "ENDPOINTID2",
                "MacAddress": "MacAddress2",
                "IPv4Address": "0.0.0.0/2",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {}
    }
]

This is normal for manually disconnecting if I have only a few containers, but if I have 50 containers, I have a problem. How to disconnect all containers from this network using one or more commands?

+4
source share
1 answer

docker network inspecthas a format .

This means that you can list all container names:

docker network inspect -f '{{range .Containers}}{{.Name}}{{end}}' network_name

, script, docker network disconnect.

wwerner :

for i in ` docker network inspect -f '{{range .Containers}}{{.Name}} {{end}}' network_name`; do docker network disconnect -f network_name $i; done;

:

for i in ` docker network inspect -f '{{range .Containers}}{{.Name}} {{end}}' network_name`;\
  do \
     docker network disconnect -f network_name $i; \
  done;

:

, , .

+5

Source: https://habr.com/ru/post/1664023/


All Articles