首页 > 解决方案 > 如何在 MSSQL Server 连接字符串中从 docker 容器引用主机端口?

问题描述

编排:

起初我只是将连接字符串保留为,.,1444但意识到容器必须在自身外部查找才能获取所需的主机localhost。

到目前为止,我已经尝试过:

我不断收到此错误:

     Connection id "0HLV8EI9HSV9D", Request id "0HLV8EI9HSV9D:00000009": An unhandled exception was thrown by the application.
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 accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: TCP Provider, error: 40 - Could not open a connection to SQL Server)```


标签: windows.net-corenetwork-programmingsql-server-2017docker-desktop

解决方案


最常见的方法是添加 --network="host"docker run 命令,然后 docker 容器中的 127.0.0.1 将指向您的 docker 主机。

如果这不适合你这个问题包含了一堆 hacky 方法来实现同样的事情。

如:

  • 映射容器中的本地 IP 别名 (DNS)
  • 出于通信目的卷挂载某些文件
  • 为 docker0 接口启用 route_localnet

我相信该链接将帮助您找到最适合您的解决方案。


推荐阅读