windows - 如何在 MSSQL Server 连接字符串中从 docker 容器引用主机端口?
问题描述
编排:
- 视窗主机
- 我的数据库在 localhost:1444 的容器中运行。
- 我的 API 在 localhost:5000/5001 上的容器中运行。
- 它们都在默认 (
bridge
) 网络上运行。 - 我不能使用自定义网络,因为目前我们的应用引用了另一个不在容器中的 API
起初我只是将连接字符串保留为,.,1444
但意识到容器必须在自身外部查找才能获取所需的主机localhost。
到目前为止,我已经尝试过:
127.0.0.1:1444
host.docker.internal:1444
127.0.0.1,1444
host.docker.internal,1444
.,1444
我不断收到此错误:
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)```
解决方案
最常见的方法是添加 --network="host"
docker run 命令,然后 docker 容器中的 127.0.0.1 将指向您的 docker 主机。
如果这不适合你,这个问题包含了一堆 hacky 方法来实现同样的事情。
如:
- 映射容器中的本地 IP 别名 (DNS)
- 出于通信目的卷挂载某些文件
- 为 docker0 接口启用 route_localnet
我相信该链接将帮助您找到最适合您的解决方案。
推荐阅读
- html - 无法在此下拉菜单上应用过渡
- java - Gradle:如何为所有子项目配置插件扩展(例如 checkstyle {...})?
- flask - Flask - 模态表单 - 如何在模态上显示字段错误?
- python - 用硒保存图像?
- javascript - 为什么我的 gatsby 应用程序显示 Unknown argument 'frontmatter' 错误?
- class - 如果我尝试在 Python3.7 的类中修改函数中的当前对象,那么为什么这些更改没有反映在函数调用之外?
- sql - 使用 Join 消除数据
- webstorm - WebStorm 实际上没有保存文件?
- python - 如何将字符串转换为货币价值?
- git - 我被 git bash 致命错误报告卡住了