c# - .Net Core Docker镜像在Docker中运行时无法访问数据库

标签 c# docker

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

相关文章:

ubuntu - ubuntu 12.04 上的 Docker 守护进程启动错误

wordpress - 使用docker-compose将WordPress命名卷挂载到NGINX容器的子目录中

c# - 在 StringTemplate 4 中使用逗号作为列表分隔符

c# - Sitecore 创建包含字段 [] 的项目

c# - ORA-06502、ORA-06512 Oracle 存储过程错误

docker - 来自守护程序 : attaching to network failed, 的错误响应请确保您的网络选项正确并检查管理器日志:超出了上下文截止日期

python - 使用docker在heroku中部署Django api rest

docker - keycloak 动态客户端注册registration_client_uri docker主机名覆盖

c# - 如何连接两个类型为 List<KeyValuePair<string,string>> 的 C# 列表。我有一些重复值,我需要这些重复值

javascript - 如何从代码隐藏文件调用 javascript 客户端函数