我是 Docker 的新手,我們正在將作業中的 ASP.NET CORE 應用程式遷移到公司的 Docker
問題是我沒有找到任何有關如何從容器連接到外部已經存在的 MS SQL 服務器 [不在 docker 映像中] 資料庫的相關主題。所有主題都是關于連接到MS SQL-Server 的官方影像[我不需要]。
我寫了 Dockerfile 和應用程式正在運行,但沒有連接到 SQL Server
請給我正確的方向與主題或提示謝謝!
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)
uj5u.com熱心網友回復:
Docker 提供了自己的內部 DNS 服務,可以將其他容器名稱決議為主機名。看起來您已將資料庫位置配置為裸主機名server02。這首先命中 Docker DNS 決議器,并嘗試將其決議為容器名稱;當失敗時,這就是你得到錯誤的原因。
在不同的環境中,您可以通過“名稱決議失敗”或“沒有這樣的主機”等錯誤訊息來區分不同型別的錯誤;這與“連接被拒絕”或“對等連接重置”之類的錯誤訊息不同。
您可以通過使用完全限定域名 (FQDN) 作為資料庫位置來解決此問題;例如,server02.example.com而不僅僅是server02. 由于這看起來不像容器名稱,它會導致 Docker 將 DNS 請求轉發到您的普通名稱服務器。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/486775.html
標籤:sql服务器 码头工人 asp.net 核心 码头工人撰写
上一篇:ASP.NETCore6,Angular和REST前綴(使用Microsoft.AspNetCore.SpaProxy)
下一篇:在多個類中使用單個類實體
