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

容器化Blazor应用无法通过容器名连接SQL Server容器问题排查

Blazor + Docker Compose 连接SQL Server容器失败问题排查

我在学习Docker Compose时,基于Blazor开发了一个简单的待办事项应用,架构仅包含Blazor应用容器与SQL Server容器,但在连接数据库时持续遇到错误:

Microsoft.Data.SqlClient.SqlException (0x80131904): A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: TCP Provider, error: 40 - Could not open a connection to SQL Server: Could not open a connection to SQL Server)

当前docker-compose.yml配置

version: '3.4'

services: 
  mssql:
    image: mcr.microsoft.com/mssql/server:2022-latest
    container_name: mssql
    environment:
      - SA_PASSWORD=Str0ngP@ssword
      - ACCEPT_EULA=Y
    ports:
      - 1433:1433
    expose:
      - 1433
  blazor_playground:
    image: blazor-playground
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - 80:80
    depends_on:
      - mssql

Program.cs中的连接字符串配置

const string CONNECTION_STRING = "Server=mssql,1433; Database=playground; User Id=sa;Password=Str0ngP@ssword; Integrated Security=false; TrustServerCertificate=True;";
builder.Services.AddDbContextFactory<TodoContext>(
    options => options.UseSqlServer(CONNECTION_STRING)
);

已确认的信息

  • 可通过SSMS连接SQL Server容器,并在其中手动创建了playground数据库与TodoItems表
  • 单独运行SQL Server容器时,本地Blazor应用将连接字符串中的mssql替换为localhost即可正常连接

更新:Docker网络检查结果

执行命令 docker inspect blazorserverplayground_default 得到以下网络信息:

$ docker inspect blazorserverplayground_default
[
    {
        "Name": "blazorserverplayground_default",
        "Id": "bd8d031d4fc7f2d49fa934e0f1beb1f633284ff60a3c0048740f9d367cb15b6e",
        "Created": "2023-01-25T22:51:53.990730099Z",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "172.24.0.0/16",
                    "Gateway": "172.24.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "9de8742b7f96e0b55109e2141a54652d1403edeb9b3efbafeba8e8c69d45318a": {
                "Name": "blazorserverplayground-blazor_playground-1",
                "EndpointID": "925b031756389ee29fdc037613ea4eb2115891d658e772d25ee4d6dfc1e4d800",
                "MacAddress": "02:42:ac:18:00:03",
                "IPv4Address": "172.24.0.3/16",
                "IPv6Address": ""
            },
            "e185c2886c551ad3ca00fe915a59a061ff46878f2dbc55d5924d4b94ad2879d7": {
                "Name": "mssql",
                "EndpointID": "93480cc94dbf21ea08d0123fefccbd7ba30b4e3077b7fef6758552147d6c30c1",
                "MacAddress": "02:42:ac:18:00:02",
                "IPv4Address": "172.24.0.2/16",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {
            "com.docker.compose.network": "default",
            "com.docker.compose.project": "blazorserverplayground",
            "com.docker.compose.version": "2.15.1"
        }
    }
]

SQL Server容器名称配置正确,但DNS解析失败原因不明,请问该如何修复或进一步排查?


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.03 12:51:56