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

本文涉及的产品
容器镜像服务 ACR,镜像仓库100个 不限时长
简介: Docker 根据网络名称批量断开与之相连的容器shell实现

实践环境

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
目录
相关文章
|
9天前
|
人工智能 弹性计算 运维
ACK Edge与IDC:高效容器网络通信新突破
本文介绍如何基于ACK Edge以及高效的容器网络插件管理IDC进行容器化。
|
5天前
|
NoSQL 关系型数据库 MySQL
《docker高级篇(大厂进阶):4.Docker网络》包括:是什么、常用基本命令、能干嘛、网络模式、docker平台架构图解
《docker高级篇(大厂进阶):4.Docker网络》包括:是什么、常用基本命令、能干嘛、网络模式、docker平台架构图解
91 56
《docker高级篇(大厂进阶):4.Docker网络》包括:是什么、常用基本命令、能干嘛、网络模式、docker平台架构图解
|
12天前
|
监控 NoSQL 时序数据库
《docker高级篇(大厂进阶):7.Docker容器监控之CAdvisor+InfluxDB+Granfana》包括:原生命令、是什么、compose容器编排,一套带走
《docker高级篇(大厂进阶):7.Docker容器监控之CAdvisor+InfluxDB+Granfana》包括:原生命令、是什么、compose容器编排,一套带走
142 77
|
20天前
|
监控 Docker 容器
在Docker容器中运行打包好的应用程序
在Docker容器中运行打包好的应用程序
|
4天前
|
关系型数据库 应用服务中间件 PHP
实战~如何组织一个多容器项目docker-compose
本文介绍了如何使用Docker搭建Nginx、PHP和MySQL的环境。首先启动Nginx容器并查看IP地址,接着启动Alpine容器并安装curl测试连通性。通过`--link`方式或`docker-compose`配置文件实现服务间的通信。最后展示了Nginx配置文件和PHP代码示例,验证了各服务的正常运行。
20 3
实战~如何组织一个多容器项目docker-compose
|
14天前
|
数据建模 应用服务中间件 nginx
docker替换宿主与容器的映射端口和文件路径
通过正确配置 Docker 的端口和文件路径映射,可以有效地管理容器化应用程序,确保其高效运行和数据持久性。在生产环境中,动态替换映射配置有助于灵活应对各种需求变化。以上方法和步骤提供了一种可靠且易于操作的方案,帮助您轻松管理 Docker 容器的端口和路径映射。
55 3
|
20天前
|
负载均衡 网络协议 算法
Docker容器环境中服务发现与负载均衡的技术与方法,涵盖环境变量、DNS、集中式服务发现系统等方式
本文探讨了Docker容器环境中服务发现与负载均衡的技术与方法,涵盖环境变量、DNS、集中式服务发现系统等方式,以及软件负载均衡器、云服务负载均衡、容器编排工具等实现手段,强调两者结合的重要性及面临挑战的应对措施。
49 3
|
20天前
|
存储 安全 数据安全/隐私保护
Docker 容器化应用管理更加高效,但数据安全和业务连续性成为关键。
在数字化时代,Docker 容器化应用管理更加高效,但数据安全和业务连续性成为关键。本文探讨了 Docker 应用的备份与恢复策略,涵盖备份的重要性、内容、方法及常见工具,制定备份策略,恢复流程及注意事项,并通过案例分析和未来趋势展望,强调备份与恢复在保障应用安全中的重要性。
25 2
|
20天前
|
存储 安全 数据中心
Docker 容器凭借轻量级和高效的特性,成为应用部署的重要工具
Docker 容器凭借轻量级和高效的特性,成为应用部署的重要工具。本文探讨了 Docker 如何通过 Namespace 和 Cgroups 实现 CPU、内存、网络和存储资源的隔离,提高系统安全性和资源利用率,以及面临的挑战和应对策略。
36 1
|
20天前
|
存储 缓存 监控
Docker容器性能调优的关键技巧,涵盖CPU、内存、网络及磁盘I/O的优化策略,结合实战案例,旨在帮助读者有效提升Docker容器的性能与稳定性。
本文介绍了Docker容器性能调优的关键技巧,涵盖CPU、内存、网络及磁盘I/O的优化策略,结合实战案例,旨在帮助读者有效提升Docker容器的性能与稳定性。
54 7
下一篇
DataWorks