【问题标题】:SQL Server docker cannot be accessed in .NET Application无法在 .NET 应用程序中访问 SQL Server docker
【发布时间】:2021-10-31 14:14:44
【问题描述】:

我有一个与 SQL Server 一起运行的 .NET Core 应用程序。当我使用从 Microsoft 文档中获取的连接字符串 server=(localdb)\\MSSQLLocalDB;database=Innovent;Trusted_Connection=true; 运行我的应用程序时,它运行良好。

但是,当我尝试通过 docker 运行 SQL Server 时,我收到了这个错误 A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections..

这是连接字符串Server={server},{port};Initial Catalog={database};User ID ={user};Password={password};Trusted_Connection=true;。其中服务器是localhost,端口是1433,我的数据库名称是Innovent,用户是SA。

当我运行 docker 容器时,我可以从 Microsoft SQLServer Management Studio 远程访问服务器,如下图所示

谁能帮我知道我做错了什么?

如果您需要更多信息,请告诉我

【问题讨论】:

  • 在 wsl2 上运行 sql-server docker 的位置?
  • @arief21 我在 windows atm 上运行我的 sql-server。但我打算在 linux 上发布它
  • 连接字符串中{server} 的真正价值是什么?本地主机、网络机器名还是什么?
  • @AndrewSilver 值是本地主机。我已经想通了。服务器应该是我的机器 IP 地址而不是 localhost

标签: sql-server docker .net-core


【解决方案1】:

“localhost”在 docker 容器中不起作用,因为 docker 网络是一个单独的网络,而“localhost”只是“这台 PC”的保留名称。
尝试使用“host.docker.internal”,这样的记录被“Docker for windows”安装程序添加到“hosts”文件中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-01-22
    • 1970-01-01
    • 1970-01-01
    • 2015-03-25
    • 1970-01-01
    • 1970-01-01
    • 2018-01-31
    • 1970-01-01
    相关资源
    最近更新 更多