我有一个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): 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容器不了解连接字符串,例如
localhost
localhost\MyInstance
您需要这样的东西:Data Source=host.docker.internal:1433;DatabaseName=MyDatabase;
要么
"server=host.docker.internal\MyInstance;database=MyDatabase;"
您可能还需要使用sql身份验证,才能“从docker”跳转到本地计算机。
您必须将正在运行的docker镜像视为另一台机器,因此,如果您有localhost \ MyInstance sql服务器,则还必须打开与其的远程tcp连接。
一般经验法则。
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身份验证,则可能会收到如下错误:
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机器的带有书签的文章。
https://knowledgebase.apexsql.com/configure-remote-access-connect-remote-sql-server-instance-apexsql-tools/
关于c# - .Net Core Docker镜像在Docker中运行时无法访问数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55184930/