.Net Core Docker Image在docker中运行时无法访问数据库

2024-01-03

我有一个尝试访问数据库的 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 时,它在我的机器上本地运行良好。

Error:

System.Data.SqlClient.SqlException (0x80131904): 建立与 SQL Server 的连接时发生与网络相关或特定于实例的错误。找不到服务器或无法访问服务器 兄弟姐妹。验证实例名称是否正确以及 SQL Server 是否配置为允许远程连接。 (提供商:SQL 网络接口,错误:0 - 访问被拒绝。) ---> System.Com ponentModel.Win32Exception (5):访问被拒绝


您正在(运行)一个 docker-image ..并且(正在运行)docker-image 不理解连接字符串,例如

localhost

localhost\MyInstance

您需要 docker-running-image 才能与您的本地设置进行对话。您可以使用本地计算机的 IP 地址。但幸运的是,docker 有一个内置的 ALIAS,用于“生成我的主机(docker 镜像)”。

see: https://docs.docker.com/desktop/networking/#i-want-to-connect-from-a-container-to-a-service-on-the-host 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;

or

“服务器=主机.docker.internal\MyInstance;数据库=MyDatabase;”

您可能还需要使用 sql 身份验证,以“从 docker”跳转到本地计算机。 (docker-running-image不会理解本地开发人员对集成安全性的理解中的“集成安全性”)

您必须将运行映像的 docker 视为另一台机器,因此如果您有 localhost\MyInstance sql 服务器,则还必须打开到它的远程 tcp 连接。

一般经验法则。

在您的计算机上运行的容器...不像本地代码...当它 涉及诸如 db-server、local-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).

BONUS:

下面是我添加了书签的文章,用于允许远程连接到您的本地计算机。

https://knowledgebase.apexsql.com/configure-remote-access-connect-remote-sql-server-instance-apexsql-tools/ https://knowledgebase.apexsql.com/configure-remote-access-connect-remote-sql-server-instance-apexsql-tools/

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

.Net Core Docker Image在docker中运行时无法访问数据库 的相关文章

随机推荐