You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在已有Docker网络中实现容器间的正常通信?

Docker容器间网络连接问题求助

现有配置

我的docker-compose.yml配置如下:

version: "3.3"
services:
  client:
    container_name: client
    image: client
    build: 
      context: .
    networks:
      - test_net
    volumes:
      - ./:/client/code
  timescaledb:
    container_name: timescaledb
    image: 'timescale/timescaledb-ha:pg14-latest'
    networks:
      - test_net
    ports:
      - '9999:5432'
    volumes:
      - timescaledb:/home/postgres/pgdata/data
      - ./scripts/:/home/postgres/scripts/
      - ./scripts/init/:/docker-entrypoint-initdb.d/

volumes:
  timescaledb:
networks:
  test_net:
    name: test_net
    external: true

test_net是我通过docker network create test_net创建的自定义桥接网络,client容器基于python:3.10.0-slim-buster镜像,需要和同网络下的timescaledb(PostgreSQL)容器建立连接。通过docker inspect timescaledb查到其IP为172.20.0.3。

操作步骤

  1. docker compose up timescaledb
  2. docker compose up client

连接错误

client容器使用SQLAlchemy的create_engine,通过连接字符串"postgresql://user:password@172.20.0.3:9999"尝试连接,始终超时,错误信息:

client  | sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) could not connect to server: Connection timed out
client  |        Is the server running on host "172.20.0.3" and accepting
client  |        TCP/IP connections on port 9999?
sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) could not connect to server: Connection timed out
        Is the server running on host "172.20.0.3" and accepting
        TCP/IP connections on port 9999?

我推测是网络配置或端口映射问题,但无法定位。

更新排查信息

尝试从client容器pingtimescaledb同样超时,确认问题与网络相关:

$ docker exec -it client ping timescaledb
PING timescaledb (172.20.0.3) 56(84) bytes of data.

^C
--- timescaledb ping statistics ---
80 packets transmitted, 0 received, 100% packet loss, time 967ms

docker network inspect test_net输出:

[
    {
        "Name": "test_net",
        "Id": "0df6af7ab0718df178aafa083ee131f741d73ae553b9022b2db6b8c7fb441569",
        "Created": "2023-01-16T14:09:03.721731326-05:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "172.20.0.0/16",
                    "Gateway": "172.20.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "6b692ef3a1bdfe6a7aec34f17fcc4ad7d9b829bc0664c3be745cd80ccf8b1acc": {
                "Name": "client",
                "EndpointID": "7e4736c818686be4616b3f262f329dfd51acb34c125a3f217ce39078b51604a4",
                "MacAddress": "02:42:ac:14:00:02",
                "IPv4Address": "172.20.0.2/16",
                "IPv6Address": ""
            },
            "a3eb16c81ea134ffdbcd1ead2801e7995ee95e063220bf5eccb5469633399bac": {
                "Name": "timescaledb",
                "EndpointID": "1938fdf71a59def5c0732a17219eedfef9dac7299699688bced6065480ac5142",
                "MacAddress": "02:42:ac:14:00:03",
                "IPv4Address": "172.20.0.3/16",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {}
    }
]

恳请提供解决指导!


内容的提问来源于stack exchange,提问作者abscond

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 14:21:03