簡體   English   中英

如何從 MSSQL Server 連接字符串中的 docker 容器引用主機端口?

[英]How to reference host port from docker container in MSSQL Server connection string?

編排:

  • Windows主機
  • 我的數據庫在 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)```


最常見的方法是在 docker 運行命令中添加--network="host" ,然后在 docker 容器中添加 127.0.0.1 將指向您的 Z05B6053C41A2130AFD6FC3B158BDA 主機。

如果這不適合你這個問題包含了一堆 hacky 方法來實現同樣的事情。

如:

  • 映射容器中的本地 IP 別名(DNS)
  • 出於通信目的卷掛載某些文件
  • 為 docker0 接口啟用 route_localnet

我相信該鏈接將幫助您找到最適合您的解決方案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM