gpt4 book ai didi

bash - Docker 断开所有容器与 docker 网络的连接

转载 作者:IT老高 更新时间:2023-10-28 21:43:31 35 4
gpt4 key购买 nike

我有 docker 网络“my_network”。我想用 docker network rm my_network 删除这个 docker 网络。在此之前,我应该断开所有容器与该网络的连接。我可以使用 docker network inspect 并获得类似的输出

[
{
"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": {}
}
]

如果我只有几个容器,可以手动断开连接,但如果我有 50 个容器,我就会遇到问题。如何使用单个或多个命令断开所有容器与该网络的连接?

最佳答案

docker network inspect有一个 format option .

这意味着您可以列出所有容器名称:

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

这应该很容易通过脚本读取每个名称并调用 docker network disconnect .

wwerner下面建议in the comments以下命令:

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;

添加:

Note that there is a space in the format as opposed to the answer to split the names by a space.

关于bash - Docker 断开所有容器与 docker 网络的连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41194602/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com