簡體   English   中英

如何從容器連接外部 MS SQL 服務器數據庫

[英]How to connect external MS SQL server database from container

我是 Docker 的新手,我們正在將我們工作的 ASP.NET CORE 應用程序遷移到公司的 Docker

問題是我沒有找到任何有關如何從容器連接到外部現有 MS SQL 服務器 [不在 docker 圖像中] 數據庫的相關主題。 所有主題都是關於連接到MS SQL-Server [我不需要] 的官方圖像

我寫了 Dockerfile 並且應用程序正在運行但沒有連接到 SQL 服務器

請給我正確的方向與主題或提示謝謝!

Dockerfile

FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS base
WORKDIR /app
EXPOSE 5000 //port to app
EXPOSE 1433 //SQL-Server port


FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build
WORKDIR /src
COPY UploadCore/UploadCore.csproj UploadCore/
RUN dotnet restore UploadCore/UploadCore.csproj
COPY . .
WORKDIR /src/UploadCore
RUN dotnet build UploadCore.csproj -c Release -o /app

FROM build AS publish
RUN dotnet publish UploadCore.csproj -c Release -o /app

FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "UploadCore.dll"]

我正在運行我的應用程序,如下所示

docker run -it --rm -p 5000:5000 -p 1433:1433 --name UploadCore my-app_test:4.5

錯誤:

Microsoft.EntityFrameworkCore.Database.Connection[20004]
      An error occurred using the connection to database 'MIS_REPORTS' on server 'server02'.
fail: Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1]
      An unhandled exception has occurred while executing the request.
Microsoft.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: SQL Network Interfaces, error: 25 - Connection string is not valid: Connection string is not valid)
   at Microsoft.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at Microsoft.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
   at Microsoft.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternalConnectionTds connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire, Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity, Boolean withFailover, SqlAuthenticationMethod authType)
   at Microsoft.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, Boolean withFailover)
   at Microsoft.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString connectionOptions, SqlCredential credential, TimeoutTimer timeout)
   at Microsoft.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(TimeoutTimer timeout, SqlConnectionString connectionOptions, SqlCredential credential, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance)
   at Microsoft.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, Boolean applyTransientFaultHandling, DbConnectionPool pool)

Docker 提供了自己的內部 DNS 服務,可以將其他容器名稱解析為主機名。 看起來您已將數據庫位置配置為裸主機名server02 這首先命中 Docker DNS 解析器,並嘗試將其解析為容器名稱; 當失敗時,這就是你得到錯誤的原因。

在不同的環境中,您可以通過“名稱解析失敗”或“沒有這樣的主機”等錯誤消息來區分不同類型的錯誤; 這與“連接被拒絕”或“對等連接重置”之類的錯誤消息不同。

您可以通過使用完全限定域名 (FQDN) 作為數據庫位置來解決此問題; 例如, server02.example.com而不是server02 由於這看起來不像容器名稱,它會導致 Docker 將 DNS 請求轉發到您的普通名稱服務器。

同樣的問題困擾了我好幾天,終於能夠按照以下步驟解決它。

在我的例子中,SQL 服務器托管在 Azure 托管實例上,並暴露了一個私有端點。 docker 能夠將 Azure 托管實例的 DNS 名稱解析到私有 ZA12A3079E14CED46E69B2 地址(如預期)。 But this resolved IP was considered by the docker network/bridge as a locally running application because upon inspecting docker's bridge ( docker network inspect bridge )The subnet of the bridge was found to be a range of IP addresses where the resolved IP of the SQL server也跌倒。 因此,docker 假定 SQL 服務器托管在同一本地網絡中,而不是外部(此處為企業網絡)。 就我而言,我正在工作的 Windows VM 已經在企業的專用網絡上。

解決方案:

In the docker desktop -> settings -> Docker Engine json file, add a new key value pair "bip": "<local IP address range which is outside of the resolved sql server IP>" . 重新啟動 docker 桌面,問題應該得到解決。

暫無
暫無

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

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