<small id='ygHlt'></small><noframes id='ygHlt'>

<tfoot id='ygHlt'></tfoot>
    • <bdo id='ygHlt'></bdo><ul id='ygHlt'></ul>

      <legend id='ygHlt'><style id='ygHlt'><dir id='ygHlt'><q id='ygHlt'></q></dir></style></legend>

      <i id='ygHlt'><tr id='ygHlt'><dt id='ygHlt'><q id='ygHlt'><span id='ygHlt'><b id='ygHlt'><form id='ygHlt'><ins id='ygHlt'></ins><ul id='ygHlt'></ul><sub id='ygHlt'></sub></form><legend id='ygHlt'></legend><bdo id='ygHlt'><pre id='ygHlt'><center id='ygHlt'></center></pre></bdo></b><th id='ygHlt'></th></span></q></dt></tr></i><div id='ygHlt'><tfoot id='ygHlt'></tfoot><dl id='ygHlt'><fieldset id='ygHlt'></fieldset></dl></div>

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

        时间:2023-06-25

            <bdo id='19ZUs'></bdo><ul id='19ZUs'></ul>
            1. <i id='19ZUs'><tr id='19ZUs'><dt id='19ZUs'><q id='19ZUs'><span id='19ZUs'><b id='19ZUs'><form id='19ZUs'><ins id='19ZUs'></ins><ul id='19ZUs'></ul><sub id='19ZUs'></sub></form><legend id='19ZUs'></legend><bdo id='19ZUs'><pre id='19ZUs'><center id='19ZUs'></center></pre></bdo></b><th id='19ZUs'></th></span></q></dt></tr></i><div id='19ZUs'><tfoot id='19ZUs'></tfoot><dl id='19ZUs'><fieldset id='19ZUs'></fieldset></dl></div>
                • <small id='19ZUs'></small><noframes id='19ZUs'>

                    <tbody id='19ZUs'></tbody>
                  <legend id='19ZUs'><style id='19ZUs'><dir id='19ZUs'><q id='19ZUs'></q></dir></style></legend>
                • <tfoot id='19ZUs'></tfoot>
                  本文介绍了ASP.NET Core Web API 服务无法连接到同一网络上的 Docker SQL Server的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个 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.

                    这篇关于ASP.NET Core Web API 服务无法连接到同一网络上的 Docker SQL Server的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:SQL Server Always Encrypted 与 .NET Core 不兼容 下一篇:.net core 2.1 MVC for MySQL 数据库中的瞬时故障处理

                  相关文章

                • <legend id='T2p46'><style id='T2p46'><dir id='T2p46'><q id='T2p46'></q></dir></style></legend>
                  <i id='T2p46'><tr id='T2p46'><dt id='T2p46'><q id='T2p46'><span id='T2p46'><b id='T2p46'><form id='T2p46'><ins id='T2p46'></ins><ul id='T2p46'></ul><sub id='T2p46'></sub></form><legend id='T2p46'></legend><bdo id='T2p46'><pre id='T2p46'><center id='T2p46'></center></pre></bdo></b><th id='T2p46'></th></span></q></dt></tr></i><div id='T2p46'><tfoot id='T2p46'></tfoot><dl id='T2p46'><fieldset id='T2p46'></fieldset></dl></div>

                  <small id='T2p46'></small><noframes id='T2p46'>

                    <tfoot id='T2p46'></tfoot>
                      • <bdo id='T2p46'></bdo><ul id='T2p46'></ul>