Docker 根据网络名称批量断开与之相连的容器shell实现

  • A+
所属分类:linux技术
摘要

Centos7Docker 20.10.5使用 docker-compose down 命令关闭容器时,提示类似以下错误:查看网络信息


实践环境

Centos7

Docker 20.10.5

问题描述

使用 docker-compose down 命令关闭容器时,提示类似以下错误:

Removing network xxx_default ERROR: network xx_default id cec096539c911cc9c2f79a6e2152c9297c4f13778a05cce3f4beb85d78c02599 has active endpoints 

解决方法

查看网络信息

# docker network ls NETWORK ID     NAME                   DRIVER    SCOPE 3697abff8144   bridge                 bridge    local cec096539c91   testproject_default    bridge    local 43662a8e3234   host                   host      local 9097274015ae   none                   null      local # docker inspect testproject_default [     {         "Name": "testproject_default",         "Id": "cec096539c911cc9c2f79a6e2152c9297c4f13778a05cce3f4beb85d78c02599",         "Created": "2021-03-09T12:38:18.283202946+08:00",         "Scope": "local",         "Driver": "bridge",         "EnableIPv6": false,         "IPAM": {             "Driver": "default",             "Options": null,             "Config": [                 {                     "Subnet": "172.20.0.0/16",                     "Gateway": "172.20.0.1"                 }             ]         },         "Internal": false,         "Attachable": true,         "Ingress": false,         "ConfigFrom": {             "Network": ""         },         "ConfigOnly": false,         "Containers": {             "0aa99e2216c58639789efbc4078d4c0f0b185ae03a0f86b245fa8e43bed9e43c": {                 "Name": "test_factory_automation_queue.celery",                 "EndpointID": "bdb6a0f2d51591af4dc193f44ad436815b36db0085bde3c848067d6952985510",                 "MacAddress": "02:42:ac:14:00:06",                 "IPv4Address": "172.20.0.6/16",                 "IPv6Address": ""             },             "35bd5e905655313950df7a7b28c57d2db0be7a3b1f409e38780e60f7561c0dbd": {                 "Name": "be.api",                 "EndpointID": "34ac5cbfac50b0da37d84d4b952f2edda572858248c1d9bb8b1beae47ce600bf",                 "MacAddress": "02:42:ac:14:00:0f",                 "IPv4Address": "172.20.0.15/16",                 "IPv6Address": ""             },             "493e980856cc0a230dd03983421583dcbc54dc63af7b882f1f17c69e1b4d1ae2": {                 "Name": "code_statistics_exec_queue.celery",                 "EndpointID": "a3c96665fff6f1e2003be0ed094733bc1cc44012582672671642af56dec5b783",                 "MacAddress": "02:42:ac:14:00:02",                 "IPv4Address": "172.20.0.2/16",                 "IPv6Address": ""             },             "4be82902bbef48a5997e6254543d3a6aba94b6eb0afc9c5bfa98c9e39437d886": {                 "Name": "task.monitor",                 "EndpointID": "05c7350beb5c52a0013119149f1075d9527dfd7c1e1775b0d50c84c76eca4466",                 "MacAddress": "02:42:ac:14:00:05",                 "IPv4Address": "172.20.0.5/16",                 "IPv6Address": ""             },             "4eaf87b0571e3476d13daedbdf4492c4ef8df49939926d7397ce9431b83475b6": {                 "Name": "be.beat",                 "EndpointID": "bb3bc019b3338603bf07991822ae984c6ff5ecc2ddc9daf20e8d8196ea66f68b",                 "MacAddress": "02:42:ac:14:00:07",                 "IPv4Address": "172.20.0.7/16",                 "IPv6Address": ""             },             "5377c1a27d4842878627e5c90f6abd4bcda0b2cb9a90ba2f09c31180cb59d3f7": {                 "Name": "temporary_queue.celery",                 "EndpointID": "eda6c7912ce0b96cd45de5a5b83cd3b8db36a0589da78d1f0b75feec43388b62",                 "MacAddress": "02:42:ac:14:00:08",                 "IPv4Address": "172.20.0.8/16",                 "IPv6Address": ""             },             "6e50c145637a406665d6b4d1324c037cde63fac48e55f2a66051a94f06270c4b": {                 "Name": "nginx",                 "EndpointID": "cefe0ed43174c19b54aae4f0d0692d7dbf90fe547460d55a5f0634ba2c1d11f8",                 "MacAddress": "02:42:ac:14:00:04",                 "IPv4Address": "172.20.0.4/16",                 "IPv6Address": ""             },             "8fac8fe4564f5248afe23cbf22c52404d1e15fdf6604b474bf7a913c0a5e5b79": {                 "Name": "websocket_channel_queue.celery",                 "EndpointID": "3de2141b43f42cc3697c7bcceef2f22638c8ae48b6ee4b2c9df5b2cab3a00f81",                 "MacAddress": "02:42:ac:14:00:0d",                 "IPv4Address": "172.20.0.13/16",                 "IPv6Address": ""             },             "99ea28e93ba3022fd0b4462a11c1c9ae92e6ff25a9a3728b78885cdd1a9c6781": {                 "Name": "ws.api",                 "EndpointID": "ca3a4aadb97988b2536691dad6c15b949e33a043467579d57221fc5b2f7c477b",                 "MacAddress": "02:42:ac:14:00:09",                 "IPv4Address": "172.20.0.9/16",                 "IPv6Address": ""             },             "aa37b2bc78170b39cc838011a70c8197b84faf76d5f9c25085b8de5b21cdd5cd": {                 "Name": "mdm_pretreatedData_queue.celery",                 "EndpointID": "48c9d9aaef14d18129d6f0946683173f0b75e86b80ad6c91699848454e7358c5",                 "MacAddress": "02:42:ac:14:00:10",                 "IPv4Address": "172.20.0.16/16",                 "IPv6Address": ""             },             "ad4a3f803521329f0162f9d3c501b4a07e9f9347b1fcf93434fe0ec1f65b8186": {                 "Name": "db.redis",                 "EndpointID": "053492c8ece06a983e4ceeb2680def2aa47f2474ae093e8c8e3351bff48879af",                 "MacAddress": "02:42:ac:14:00:0c",                 "IPv4Address": "172.20.0.12/16",                 "IPv6Address": ""             },             "ada7696d064713414d77b32c4fc991831bc6294a8c9c749024741fdac38ee2b2": {                 "Name": "locust_case_exec_queue.celery",                 "EndpointID": "298936d7adeabe2afcd8647ed0e7660b162e455552b4c54a724c15c2219266cf",                 "MacAddress": "02:42:ac:14:00:03",                 "IPv4Address": "172.20.0.3/16",                 "IPv6Address": ""             },             "bdf4c37f7ab88014d8e493a146255d6a2ffde50580e3164276447923147895ea": {                 "Name": "test_factory_common_queue.celery",                 "EndpointID": "ebe85ebfa63c07cf085edfafe6de26bb1f137c9dfca2506c74b4787e2b5d1f4f",                 "MacAddress": "02:42:ac:14:00:0e",                 "IPv4Address": "172.20.0.14/16",                 "IPv6Address": ""             },             "c93bfcb8ddbf66b996f3ba33ea9c8ae9bb4234001a9d7e5a708a3793876a8f31": {                 "Name": "ct_test_exec_queue.celery",                 "EndpointID": "6f267c19021416b9d03e6fe7b66bdfdd4524666f5c3e7d3ec0ae6d68a9647793",                 "MacAddress": "02:42:ac:14:00:0b",                 "IPv4Address": "172.20.0.11/16",                 "IPv6Address": ""             },             "ef73c5728e840d69c04372044545a370262f4b438c312e9fa012b249977b1f67": {                 "Name": "mdm_oe_trans_queue.celery",                 "EndpointID": "48d8c2ed1413338e2b461ee8ef497fc951e914fe698fcad311bd03f53cefd7f3",                 "MacAddress": "02:42:ac:14:00:0a",                 "IPv4Address": "172.20.0.10/16",                 "IPv6Address": ""             }         },         "Options": {},         "Labels": {             "com.docker.compose.network": "default",             "com.docker.compose.project": "testproject",             "com.docker.compose.version": "1.28.5"         }     } ] 

SHELL实现批量断开与目标网络关联的容器:

#!bin/bash for i in `docker network inspect -f '{{range .Containers}}{{.Name}} {{end}}' testproject_default`;    do       sudo docker network disconnect -f testproject_default $i;    done; if [ $? -ne 0 ]; then     echo "断开容器关联网络 testproject_default 失败"     exit 1 else     echo -e "断开容器关联网络 testproject_default 成功n" fi 

注意:

实践时发现,这样以后,有时候还是会存在上述问题问题

解决方法:重启Docker

sudo service docker restart