ASP.NET Core Web API 服务无法连接到同一网络上的 Docker SQL Server

2021-11-24 00:00:00 docker sql-server asp.net-core .net-core

我有一个 ASP.NET Core v3 Web API 服务.它可以正常运行并连接到普通的 Windows 托管 SQL Server 实例.但是当我尝试将它连接到 Docker Linux 托管的 SQL Server 实例时,它失败了.

I have a ASP.NET Core v3 Web API service. It runs and connects just fine to a normal Windows hosted SQL Server instance. But when I try to connect it to a Docker Linux hosted SQL Server Instance it fails.

以下是一些细节:

  • 两个容器都在我的 Windows 10 开发机器上运行
  • Web API 服务容器是基于 Linux 的,它本身可以正常工作
  • Linux SQL Server 容器本身运行良好
    • 我可以毫无问题地通过 SSMS 连接和运行查询.
    • 我运行了 docker network inspect bridge -f "{{json .Containers }}" 并且它列出了两个容器.
    • I ran docker network inspect bridge -f "{{json .Containers }}" and it listed both containers.

    这是我的数据库连接字符串:

    Here is my database connection string:

    "Server=localhost;Database=MyDatabase;User Id=MySqlUser;Password=MyPassword"

    "Server=localhost;Database=MyDatabase;User Id=MySqlUser;Password=MyPassword"

    我还尝试在 localsql 的 hosts 文件中创建一个条目,设置为 127.0.0.0.localhost 和 localsql 都可以使用 SSMS 连接到 docker 数据库.

    I also tried making an entry in my hosts file of localsql set to 127.0.0.0. Both localhost and localsql work fine to connect to the docker database using SSMS.

    这是实际的错误消息:

    与 SQL Server 建立连接时发生与网络相关或特定于实例的错误.服务器未找到或无法访问.验证实例名称是否正确以及 SQL Server 是否配置为允许远程连接.(提供程序:TCP 提供程序,错误:40 - 无法打开与 SQL Server 的连接)

    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)

    当我尝试从我的主机文件中使用 localsql 时,它有一个内部异常消息名称或服务未知"

    When I try using localsql from my hosts file, it has an inner exception with this message "Name or service not known"

    推荐答案

    连接字符串指向 localhost.容器中的 localhost 是指容器本身,因此 ASP.NET Core Container 会尝试在同一容器上查找 SQL Server.

    The connection string points to localhost. Localhost in a container refers to the container itself, so that the ASP.NET Core Container tries to find the SQL Server on the same container.

    在连接字符串中使用 SQL Server 容器的名称,而不是 localhost.为了使其工作,您需要创建一个用户定义的网桥,两个容器都连接到该网桥:

    Instead of localhost, use the name of the SQL Server container in the connection string. In order for this to work, you need to create a user-defined network bridge that both containers are connected to:

    docker network create sql-server-test
    

    运行容器时,您需要指定容器连接的网络,如下所示:

    When running the containers, you need to specify the network that the containers are connected to, like so:

    # Please note the --name and --network parameters
    docker run -e ACCEPT_EULA=Y -e 'SA_PASSWORD=<PWD>' -d --name sql-server --network sql-server-test mcr.microsoft.com/mssql/server:2019-latest
    # Please note the --network parameter
    docker run -d -p 8889:80 --name aspnet-core --network sql-server-test aspnet-core
    

    由于两个容器现在连接到同一个网络,你可以在连接字符串中使用Sql Server容器的名称(在同一个sql-server中),并使用这个连接字符串连接到Sql Server:

    As both containers are now connected to the same network, you can use the name of the Sql Server container (in the same sql-server) in the connection string and connect to the Sql Server using this connection string:

    "Data Source=sql-server;Initial Catalog=master;User ID=sa;Password=<PWD>"
    

    有关 Docker 网络的详细信息以及有关如何在容器之间设置通信的其他选项,请参阅此链接.

    For details on Docker networks and on other options on how to setup communication between containers, see this link.

相关文章