简体   繁体   English

无法从带有 ASP.NET Core 的 Docker 容器连接到 SQL Server 容器

[英]Can't connect from the Docker container with ASP.NET Core to SQL Server container

I have a container deploying a WEB API in ASP.NET Core trying to connect to the SQL Server database.我有一个在 ASP.NET Core 中部署 WEB API 的容器,试图连接到 SQL Server 数据库。 I am running Windows 10 with Docker Desktop.我正在使用 Docker Desktop 运行 Windows 10。

I can successfully connect to the Docker container with SQL Server from SQL Server Management Studio and my ASP.NET Core app (without container).我可以从 SQL Server Management Studio 和我的 ASP.NET Core 应用程序(没有容器)成功地连接到带有 SQL Server 的 Docker 容器。

But when I run my ASP.NET Core inside the container, I've got an error:但是当我在容器中运行我的 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)
...

docker-compose for the SQL Server: 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"

docker-compose for the WEB API:用于 WEB API 的 docker-compose:

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

Dockerfile for the WEB API: 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"]

The connection string to SQL Server: SQL Server 的连接字符串:

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

By default, containers are connected to the bridge driver.默认情况下,容器连接到网桥驱动程序。 To access sqlserver from ASP.NET, you must use the container name of the sqlserver in connection string.要从 ASP.NET 访问 sqlserver,您必须在连接字符串中使用 sqlserver 的容器名称。 Like this.像这样。

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

For more network details you can run the cmd command below.有关更多网络详细信息,您可以运行下面的 cmd 命令。

docker network inspect bridge

Sorry, It was my fail:(对不起,这是我的失败:(
I just forgot that docker-compose up -d command doesn't rebuild Docker images.我只是忘记了docker-compose up -d命令不会重建 Docker 映像。
I removed all images, and run the command again.我删除了所有图像,然后再次运行该命令。
With the configurations above it works fine for me!上面的配置对我来说很好用!

command for inspect bridge helped a lot: docker network inspect bridge检查桥的命令有很大帮助: docker network inspect bridge

i have Windows development machine to run Visual studio and docker desktop on it.我有 Windows 开发机器来运行 Visual Studio 和 docker 桌面。 so i tried to connect from one container with asp.net core app to another with ms sql running所以我尝试从一个带有asp.net core应用程序的容器连接到另一个运行ms sql的容器

i used host.docker.internal as Data source in my connection string and it worked fine我在连接字符串中使用host.docker.internal作为数据源,它运行良好

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法从 Docker (Linux) 中运行的 ASP.NET Core 连接到 SQL Server 命名实例 - Can't connect to SQL Server named instance from ASP.NET Core running in Docker (Linux) 如何从在容器中运行的 ASP.NET 核心应用程序连接到具有集成安全性的 Windows 服务器上的 SQL 服务器 - How to connect from ASP.NET Core application running in a container to SQL Server on Windows Server with Integrated Security 无法从 Docker Z9E0DA8438E1E38A1C30F4B176CE7 Core 连接到 Docker SQL 服务器 - Can not connect to Docker SQL Server from Docker ASP.NET Core 3.1 无法从docker windows容器中的asp.net核心应用程序连接到主机数据库 - Not able to connect to host database from asp.net core application which is in docker windows container ASP.Net Core docker 从 serviceB 容器访问 serviceA 容器抛出 ssl 证书错误 - ASP.Net Core docker access serviceA container from serviceB container throws ssl certificate error 无法从 Docker 中的 ASP.NET Core 3.1 连接到本地 SQL Server - Cant connect to on premise SQL Server from ASP.NET Core 3.1 in Docker 无法从 docker 容器连接到 SQL Server - Unable to connect to SQL Server from a docker container 从 docker 容器连接到 SQL Server 数据库 - Connect to SQL Server database from a docker container 无法让我的 C# / ASP.NET Core 6 MVC 应用程序通过 docker 容器在 ZF9D5C19A7F42220333 上工作 - Can't get my C# / ASP.NET Core 6 MVC app to work via a docker container over ssl Asp.net Core和Entity Framework无法连接到SQL Server - Asp.net Core and Entity Framework can't connect to SQL Server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM