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

.NET API容器运行无法连接Docker化PostgreSQL问题求助

.NET API容器化后无法连接Docker PostgreSQL数据库排查

问题场景

本地用IIS Express运行.NET API时,使用以下连接字符串可正常连接Docker化的PostgreSQL:

"ConnectionStrings": {
  "DefaultConnection": "Host=localhost;Port=5433;Database=postgres;Username=postgres;Password=1"
},

但将API容器化运行后,使用以下连接字符串时出现**TypeError: NetworkError when attempting to fetch resource.**错误:

"ConnectionStrings": {
  "DefaultConnection": "Host=postgres-db;Port=5432;Database=postgres;Username=postgres;Password=1"
},

当前已配置信息

Docker网络配置

已将API容器(Pokemon_API)和数据库容器(postgres-db)加入同一自定义网络my_network,网络详情:

C:\Users\fesat\Desktop\sample\db>docker network inspect my_network
[
    {
        "Name": "my_network",
        "Id": "736299b9a97134e8f67c5a722e343871bad2bb9fc41400105e0fbfbeaffb3fe5",
        "Created": "2025-07-09T10:04:35.298329138Z",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv4": true,
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "172.19.0.0/16",
                    "Gateway": "172.19.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "40316085dba522987ccb0c816718b8258ab14d5c2035eb69379794a8f070f4ad": {
                "Name": "Pokemon_API",
                "EndpointID": "944636efdc267e0c05f3c6b96826ed2f8f016b06a06a87f6c400c6b400cd9f13",
                "MacAddress": "ba:91:ca:cd:37:a2",
                "IPv4Address": "172.19.0.3/16",
                "IPv6Address": ""
            },
            "ce18f0f94eb76fbeb3f9d2530ca3a0bb88ef57c90ad04b91fd3053551b6d3774": {
                "Name": "postgres-db",
                "EndpointID": "1be8e2138789e2ec2cd4eac9a3bb0ae8a536eacb6733249c2c940e9d47c27c2e",
                "MacAddress": "ba:f9:9f:9e:fb:08",
                "IPv4Address": "172.19.0.2/16",
                "IPv6Address": ""
            }
        },
        "Options": {
            "com.docker.network.enable_ipv4": "true",
            "com.docker.network.enable_ipv6": "false"
        },
        "Labels": {}
    }
]

PostgreSQL容器的docker-compose.yml配置

version: '3.8'

services:
  postgres:
    image: postgres:15
    container_name: postgres-db
    restart: always
    ports:
      - "5433:5432"
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: 1
      POSTGRES_DB: postgres
    volumes:
      - pgdata:/var/lib/postgresql/data
      # - ./init.sql:/docker-entrypoint-initdb.d/init.sql  # ha szeretnél induláskor SQL-t betölteni

volumes:
  pgdata:

排查步骤

  • 验证API容器到数据库的网络连通性:进入API容器执行ping postgres-db或telnet postgres-db 5432,确认域名解析和端口可达
  • 检查API容器的配置加载情况:确认容器内的连接字符串已正确替换为容器环境版本,避免配置未更新
  • 查看PostgreSQL容器日志:执行docker logs postgres-db,确认数据库是否正常启动,是否有连接拒绝的日志记录
  • 检查PostgreSQL的pg_hba.conf配置:确认是否允许API容器所在子网(172.19.0.0/16)的连接,可添加host all all 172.19.0.0/16 md5规则后重启容器
  • 查看API容器启动日志:获取后端具体的数据库连接错误信息(如超时、认证失败),前端的NetworkError仅为表层提示,后端日志更具参考性

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.12 17:23:11