【问题标题】:Can't connect from the Docker container with ASP.NET Core to SQL Server container无法从带有 ASP.NET Core 的 Docker 容器连接到 SQL Server 容器
【发布时间】:2021-12-15 01:35:57
【问题描述】:

我有一个在 ASP.NET Core 中部署 WEB API 的容器,试图连接到 SQL Server 数据库。我正在使用 Docker Desktop 运行 Windows 10。

我可以从 SQL Server Management Studio 和我的 ASP.NET Core 应用程序(无容器)成功连接到带有 SQL Server 的 Docker 容器。

但是当我在容器中运行我的 ASP.NET Core 时,我遇到了一个错误:

fail: Microsoft.AspNetCore.Server.Kestrel[13]

      Connection id "0HMCRFGHIEO1Q", Request id "0HMCRFGHIEO1Q:00000002": An unhandled exception was thrown by the application.

      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)

         at Microsoft.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
...

用于 SQL Server 的 docker-compose:

sqlserver:
        image: mcr.microsoft.com/mssql/server:2019-latest
        ports:
            - 1433:1433
        volumes:
            - my-volume:/var/opt/mssql
        environment:
            SA_PASSWORD: "StrongPassword"
            ACCEPT_EULA: "Y"

用于 WEB API 的 docker-compose:

web_api:
        build:
            dockerfile: WebApi/Dockerfile
        ports:
            - 5000:80
        depends_on:
            - sqlserver

WEB API 的 Dockerfile:

FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base
WORKDIR /app
EXPOSE 80

FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /src
COPY ["WebApi/WebApi.csproj", "WebApi/"]
RUN dotnet restore "WebApi/WebApi.csproj"
COPY . .
WORKDIR "/src/WebApi"
RUN dotnet build "WebApi.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "WebApi.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "WebApi.dll"]

到 SQL Server 的连接字符串:

"ConnectionStrings": {
    "SqlConnectionString": "Server=192.168.0.108,1433;Database=myDb;User Id=sa;Password=StrongPassword;"
  }

【问题讨论】:

  • 请尝试用服务名或sqlserver替换你用来连接SQL服务器容器的IP
  • @NoamYizraeli,你的意思是这样的:"Server=sqlserver,1433;Database=myDb;User Id=sa;Password=StrongPassword;"?
  • 如果该配置在 .NET 容器中,那么是
  • 在同一个非默认网络中运行 2 个容器时(使用 docker compose 文件时会发生这种情况),它们可以通过服务名称而不是 ip Server=sqlserver,1433 相互访问。由于您要暴露端口 1433,因此您也可以使用 Server=host.docker.internal,1433

标签: c# asp.net sql-server docker asp.net-core


【解决方案1】:

对不起,这是我的失败:(
我只是忘记了 docker-compose up -d 命令不会重建 Docker 映像。
我删除了所有图像,然后再次运行该命令。
上面的配置对我来说很好用!

【讨论】:

    【解决方案2】:

    默认情况下,容器连接到网桥驱动程序。要从 ASP.NET 访问 sqlserver,您必须在连接字符串中使用 sqlserver 的容器名称。像这样。

    "ConnectionStrings": {
        "SqlConnectionString": "Server=sqlserver,1433;Database=myDb;User Id=sa;Password=StrongPassword;"
    }
    

    更多网络详情,您可以运行下面的 cmd 命令。

    docker network inspect bridge
    

    【讨论】:

    • 好的,docker network inspect bridge 提示很好,我可以使用它的内部 IP 地址从 ASPNET 容器“ping”SQL Server 容器......但我不能使用 NAME的容器。我需要做些什么来让容器解析其他容器的名称吗?
    猜你喜欢
    • 2019-05-29
    • 2016-12-26
    • 2022-08-16
    • 2022-01-04
    • 2020-06-12
    • 1970-01-01
    • 2019-11-19
    • 2021-01-02
    • 2018-07-28
    相关资源
    最近更新 更多