繁体   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