【Bug】docker容器之间网络通讯失败

目录

报错起因

我启动了Milvus数据库

bash 复制代码
# docker ps
CONTAINER ID   IMAGE                        COMMAND                  CREATED             STATUS                    PORTS                                                                                      NAMES
1099dda8c0fa   milvusdb/milvus:v2.4.1       "/tini -- milvus run..."   17 minutes ago      Up 17 minutes (healthy)   0.0.0.0:9091->9091/tcp, :::9091->9091/tcp, 0.0.0.0:19530->19530/tcp, :::19530->19530/tcp   milvus-standalone
acafc6ef57bc   quay.io/coreos/etcd:v3.5.5   "etcd -advertise-cli..."   17 minutes ago      Up 17 minutes (healthy)   2379-2380/tcp                                                                              milvus-etcd
587d1ee57611   minio/minio:latest           "/usr/bin/docker-ent..."   17 minutes ago      Up 17 minutes (healthy)   0.0.0.0:9000-9001->9000-9001/tcp, :::9000-9001->9000-9001/tcp                              milvus-minio

可以看到运行都很正常,但是我在另外一个容器中写代码访问19530端口,失败

另外这个容器在启动时(docker run)未设置--network host

报错内容

bash 复制代码
Traceback (most recent call last):
  File "/home/rag/startup.py", line 910, in <module>
    client = MilvusClient(uri=Milvus_Client_URI,db_name=Milvus_DB_NAME)
  File "/root/miniconda3/lib/python3.10/site-packages/pymilvus/milvus_client/milvus_client.py", line 60, in __init__
    self._using = self._create_connection(
  File "/root/miniconda3/lib/python3.10/site-packages/pymilvus/milvus_client/milvus_client.py", line 728, in _create_connection
    raise ex from ex
  File "/root/miniconda3/lib/python3.10/site-packages/pymilvus/milvus_client/milvus_client.py", line 725, in _create_connection
    connections.connect(using, user, password, db_name, token, uri=uri, **kwargs)
  File "/root/miniconda3/lib/python3.10/site-packages/pymilvus/orm/connections.py", line 449, in connect
    connect_milvus(**kwargs, user=user, password=password, token=token, db_name=db_name)
  File "/root/miniconda3/lib/python3.10/site-packages/pymilvus/orm/connections.py", line 400, in connect_milvus
    gh._wait_for_channel_ready(timeout=timeout)
  File "/root/miniconda3/lib/python3.10/site-packages/pymilvus/client/grpc_handler.py", line 150, in _wait_for_channel_ready
    raise MilvusException(
pymilvus.exceptions.MilvusException: <MilvusException: (code=2, message=Fail connecting to server on 172.17.0.2:19530, illegal connection params or server unavailable)>

这里的172.17.0.2来自于

bash 复制代码
import socket

def get_ip_address():
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    try:
        # doesn't even have to be reachable
        s.connect(('10.254.254.254', 1))
        IP = s.getsockname()[0]
    except Exception:
        IP = '127.0.0.1'
    finally:
        s.close()
    return IP

host = get_ip_address()

后面尝试更换为0.0.0.0

bash 复制代码
Traceback (most recent call last):
  File "/root/miniconda3/lib/python3.10/site-packages/pymilvus/client/grpc_handler.py", line 147, in _wait_for_channel_ready
    grpc.channel_ready_future(self._channel).result(timeout=timeout)
  File "/root/miniconda3/lib/python3.10/site-packages/grpc/_utilities.py", line 162, in result
    self._block(timeout)
  File "/root/miniconda3/lib/python3.10/site-packages/grpc/_utilities.py", line 106, in _block
    raise grpc.FutureTimeoutError()
grpc.FutureTimeoutError

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/root/miniconda3/lib/python3.10/site-packages/pymilvus/orm/connections.py", line 449, in connect
    connect_milvus(**kwargs, user=user, password=password, token=token, db_name=db_name)
  File "/root/miniconda3/lib/python3.10/site-packages/pymilvus/orm/connections.py", line 400, in connect_milvus
    gh._wait_for_channel_ready(timeout=timeout)
  File "/root/miniconda3/lib/python3.10/site-packages/pymilvus/client/grpc_handler.py", line 150, in _wait_for_channel_ready
    raise MilvusException(
pymilvus.exceptions.MilvusException: <MilvusException: (code=2, message=Fail connecting to server on 0.0.0.0:19530, illegal connection params or server unavailable)>

更换为127.0.0.1

bash 复制代码
Traceback (most recent call last):
  File "/root/miniconda3/lib/python3.10/site-packages/pymilvus/client/grpc_handler.py", line 147, in _wait_for_channel_ready
    grpc.channel_ready_future(self._channel).result(timeout=timeout)
  File "/root/miniconda3/lib/python3.10/site-packages/grpc/_utilities.py", line 162, in result
    self._block(timeout)
  File "/root/miniconda3/lib/python3.10/site-packages/grpc/_utilities.py", line 106, in _block
    raise grpc.FutureTimeoutError()
grpc.FutureTimeoutError

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/root/miniconda3/lib/python3.10/site-packages/pymilvus/orm/connections.py", line 449, in connect
    connect_milvus(**kwargs, user=user, password=password, token=token, db_name=db_name)
  File "/root/miniconda3/lib/python3.10/site-packages/pymilvus/orm/connections.py", line 400, in connect_milvus
    gh._wait_for_channel_ready(timeout=timeout)
  File "/root/miniconda3/lib/python3.10/site-packages/pymilvus/client/grpc_handler.py", line 150, in _wait_for_channel_ready
    raise MilvusException(
pymilvus.exceptions.MilvusException: <MilvusException: (code=2, message=Fail connecting to server on 127.0.0.1:19530, illegal connection params or server unavailable)>

以上都是无效的

解决方案

首先查看容器所在网段

bash 复制代码
docker network ls

得到

bash 复制代码
NETWORK ID     NAME             DRIVER    SCOPE
738c89ed339b   bridge           bridge    local
62ec1478cea1   host             host      local
5dc70a167a10   milvus           bridge    local
4d746c095f03   minio_default    bridge    local
40c6e5fbe685   mysql8_default   bridge    local
b101e25dcb4e   nginx_default    bridge    local

接下来查看milvus的IP信息

bash 复制代码
docker network inspect milvus

得到

bash 复制代码
[
    {
        "Name": "milvus",
        "Id": "5dc70a167a103d723fe81bc0feb95b996995d3d464f9601ce9c52d10342e3a6c",
        "Created": "2024-10-12T13:55:27.648549485+08:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "172.18.0.0/16",
                    "Gateway": "172.18.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": true,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "40fd07ae3cdb975afedaf9861117aae38cddb0e63c977d14891d4af860c2885d": {
                "Name": "eager_solomon",
                "EndpointID": "a9b87ca7cb773953361e58b761b20f2d90f9a0c222dc2ce1fca08238dd0cb1cf",
                "MacAddress": "02:42:ac:12:00:05",
                "IPv4Address": "172.18.0.5/16",
                "IPv6Address": ""
            },
            "6453a43e6cfa6b13b5a043bf62255447dbef1ce86876bdcdee55f6520f651bb5": {
                "Name": "milvus-minio",
                "EndpointID": "e977606fab9781521fa9dfcd8fcdec032b58daca9d7449cbc338c78ecbca1646",
                "MacAddress": "02:42:ac:12:00:03",
                "IPv4Address": "172.18.0.3/16",
                "IPv6Address": ""
            },
            "957671f5a506146d5381e26f8afc22c5d2c5566678043a054d2cc4d0febd8873": {
                "Name": "milvus-etcd",
                "EndpointID": "4c0c44610d15254f53ea9839d34bdbf841427fcf6f7ef097b82c95694b5422c4",
                "MacAddress": "02:42:ac:12:00:02",
                "IPv4Address": "172.18.0.2/16",
                "IPv6Address": ""
            },
            "e6c952ee5bd431adf2c5bb3daf643612a4811f5155c46d46ac73607a845830ee": {
                "Name": "milvus-standalone",
                "EndpointID": "9fb76ce838126cf651d43360342b96ad1184b8ba37f4ab92e022ccb5fae38cf0",
                "MacAddress": "02:42:ac:12:00:04",
                "IPv4Address": "172.18.0.4/16",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {
            "com.docker.compose.network": "milvus",
            "com.docker.compose.project": "milvus",
            "com.docker.compose.version": "1.29.2"
        }
    }
]

看到milvus-standalone的IPv4地址为172.18.0.4

最终用这个地址链接成功

相关推荐
Karoku06611 分钟前
【k8s集群应用】kubeadm1.20高可用部署(3master)
运维·docker·云原生·容器·kubernetes
saynaihe2 小时前
安全地使用 Docker 和 Systemctl 部署 Kafka 的综合指南
运维·安全·docker·容器·kafka
G_whang3 小时前
centos7下docker 容器实现redis主从同步
redis·docker·容器
认真学习的小雅兰.3 小时前
如何在Ubuntu上利用Docker和Cpolar实现Excalidraw公网访问高效绘图——“cpolar内网穿透”
linux·ubuntu·docker
the丶only4 小时前
单点登录平台Casdoor搭建与使用,集成gitlab同步创建删除账号
linux·运维·服务器·docker·gitlab
书生-w4 小时前
Docker部署GitLab服务器
服务器·docker·gitlab
塔克拉玛攻城狮4 小时前
私有网盘+在线文档:内网离线搭建NextCloud+OnlyOffice详细指南
docker·在线文档·网盘
ccubee4 小时前
docker 安装 ftp
运维·docker·容器
TsengOnce6 小时前
Docker 安装 禅道-21.2版本-外部数据库模式
运维·docker·容器
无为扫地僧7 小时前
三、ubuntu18.04安装docker
ubuntu·docker