.Net Core Docker映像在Docker中运行时无法访问数据库 [英] .Net Core Docker Image cannot access database when running in docker

查看:218
本文介绍了.Net Core Docker映像在Docker中运行时无法访问数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个dotnet核心应用程序尝试访问数据库,当我在Visual Studio中运行它时,它工作正常(可能是由于我的域身份验证),但是当我尝试构建docker映像并使用运行它时docker run -it --rm -p 8080:80 --name控制台控制台

I have a dotnet core application that tries to access to the database, when I run it in visual studio it works fine (probably because of my domain authentication) but when I try to build docker image and run it with docker run -it --rm -p 8080:80 --name console console

我遇到此错误,但我真的不明白为什么吗?有谁知道我该如何解决?

I'm getting this error, but I don't really understand why? Does anyone know how I can fix this?

连接字符串如下所示: Data Source = DBTest;初始目录=测试;集成安全性=正确

The connection string looks like this: Data Source=DBTest; Initial Catalog=test;Integrated Security=True

当我通过Visual Studio运行时,它可以在我的机器上本地正常工作.

it works fine locally on my machine when I run through visual studio.

错误:

System.Data.SqlClient.SqlException(0x80131904):建立与SQL Server的连接时发生与网络相关或特定于实例的错误.找不到服务器或未访问服务器sible.验证实例名称正确,并且已将SQL Server配置为允许远程连接.(提供者:SQL网络接口,错误:0-访问被拒绝.)---> System.ComponentModel.Win32Exception(5):访问被拒绝

System.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 acces sible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 0 - Access is denied.) ---> System.Com ponentModel.Win32Exception (5): Access is denied

推荐答案

您正在(运行中)的Docker容器不了解连接字符串,如

You are (running) docker container does NOT understand connection strings like

localhost

localhost\MyInstance

您需要这样的东西:

Data Source = host.docker.internal:1433; DatabaseName = MyDatabase;

Data Source=host.docker.internal:1433;DatabaseName=MyDatabase;

" server = host.docker.internal \ MyInstance; database = MyDatabase;"

"server=host.docker.internal\MyInstance;database=MyDatabase;"

您可能还需要使用sql身份验证,才能从docker"跳转到到您的本地计算机.

You may need to use sql authentication as well, to jump "from docker" to your local machine.

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

You have to treat the docker running images AS ANOTHER MACHINE, so if you have a localhost\MyInstance sql server, you'll have to open up remote tcp connections to it as well.

一般经验法则.

计算机上运行的容器...与本地代码不同.涉及到诸如db-server,local-sftp等之类的东西.

A container running on your machine...is NOT like local code..when it comes to hitting things like a db-server, local-sftp, etc.

如果不使用sql身份验证,则可能会收到如下错误:

If you do not use sql-authentication, you'll probably get an error like this:

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).

奖金:

下面是我加了书签的文章,允许远程连接到LOCAL机器.

Below is a bookmarked article I have for allowing remote connections to your LOCAL machine.

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

这篇关于.Net Core Docker映像在Docker中运行时无法访问数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆