我有一个尝试访问数据库的 dotnet 核心应用程序,当我在 Visual Studio 中运行它时,它工作正常(可能是因为我的域身份验证),但是当我尝试构建 docker 映像并运行它时docker run -it --rm -p 8080:80 --name console console
我收到此错误,但我不太明白为什么?有谁知道我该如何解决这个问题?
连接字符串如下所示:Data Source=DBTest; Initial Catalog=test;Integrated Security=True
当我运行 Visual Studio 时,它在我的机器上本地运行良好。
错误:
System.Data.SqlClient.SqlException (0x80131904): 建立与 SQL Server 的连接时发生网络相关或特定于实例的错误。服务器未找到或无法访问。验证实例名称是否正确以及 SQL Server 是否配置为允许远程连接。(提供程序:SQL 网络接口,错误:0 - 访问被拒绝。) ---> System.ComponentModel.Win32Exception (5):访问被拒绝
您正在(运行)一个 docker-image ..并且(正在运行)docker-image 不理解连接字符串,例如
localhost
localhost\MyInstance
Run Code Online (Sandbox Code Playgroud)
您需要 docker-running-image 才能与您的本地设置进行对话。您可以使用本地计算机的 IP 地址。但幸运的是,docker 有一个内置的 ALIAS,用于“生成我的主机(docker 镜像)”。
请参阅: https: //docs.docker.com/desktop/networking/#i-want-to-connect-from-a-container-to-a-service-on-the-host
我想从容器连接到主机上的服务 主机具有不断变化的 IP 地址(如果您没有网络访问权限,则没有 IP 地址)。我们建议您连接到特殊的 DNS 名称 host.docker.internal,该名称解析为主机使用的内部 IP 地址。这是出于开发目的,不适用于 Docker Desktop 之外的生产环境。
下面显示了该别名,以及如何将其用于 Ms-Sql-Server 连接字符串。
数据源=host.docker.internal:1433;DatabaseName=MyDatabase;
或者
“服务器=主机.docker.internal\MyInstance;数据库=MyDatabase;”
您可能还需要使用 sql 身份验证,以“从 docker”跳转到本地计算机。(docker-running-image不会理解本地开发人员对集成安全性的理解中的“集成安全性”)
您必须将运行映像的 docker 视为另一台机器,因此如果您有 localhost\MyInstance sql 服务器,则还必须打开到它的远程 tcp 连接。
一般经验法则。
在您的计算机上运行的容器...与本地代码不同...当涉及到数据库服务器、本地 sftp 等时。
如果您不使用 sql-authentication,您可能会收到如下错误:
Cannot authenticate using Kerberos. Ensure Kerberos has been initialized on the client with 'kinit' and a Service Principal Name has been registered for the SQL Server to allow Kerberos authentication.
ErrorCode=InternalError, Exception=Interop+NetSecurityNative+GssApiException: GSSAPI operation failed with error - Unspecified GSS failure. Minor code may provide more information (SPNEGO cannot find mechanisms to negotiate).
Run Code Online (Sandbox Code Playgroud)
奖金:
下面是我添加了书签的文章,用于允许远程连接到您的本地计算机。
| 归档时间: |
|
| 查看次数: |
2327 次 |
| 最近记录: |