简体   繁体   English

如何从容器连接外部 MS SQL 服务器数据库

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

I'm new in Docker and we are migrating our working ASP.NET CORE app to Docker in company我是 Docker 的新手,我们正在将我们工作的 ASP.NET CORE 应用程序迁移到公司的 Docker

Problem is I did't find any releated topics about how to connect to external already existing MS SQL server [not in docker image] database from container.问题是我没有找到任何有关如何从容器连接到外部现有 MS SQL 服务器 [不在 docker 图像中] 数据库的相关主题。 All topics are about connecting to official Image of MS SQL-Server [which I don't need].所有主题都是关于连接到MS SQL-Server [我不需要] 的官方图像

I wrote Dockerfile and application running but there no connection to SQL Server我写了 Dockerfile 并且应用程序正在运行但没有连接到 SQL 服务器

Please give me correct direction with topics or hints thank you!请给我正确的方向与主题或提示谢谢!

Dockerfile 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"]

I'm running my app as below我正在运行我的应用程序,如下所示

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

Error:错误:

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 provides its own internal DNS service that can do things like resolve other container names as host names. Docker 提供了自己的内部 DNS 服务,可以将其他容器名称解析为主机名。 It looks like you've configured your database location as a bare host name server02 .看起来您已将数据库位置配置为裸主机名server02 This hits the Docker DNS resolver first, and it tries to resolve it as a container name;这首先命中 Docker DNS 解析器,并尝试将其解析为容器名称; when that fails, that's why you're getting the error you do.当失败时,这就是你得到错误的原因。

In different environments you can tell this apart from different kinds of errors by error messages like "name resolution failed" or "no such host";在不同的环境中,您可以通过“名称解析失败”或“没有这样的主机”等错误消息来区分不同类型的错误; this is different from an error message like "connection refused" or "connection reset by peer".这与“连接被拒绝”或“对等连接重置”之类的错误消息不同。

You can resolve this by using a fully-qualified domain name (FQDN) as your database location;您可以通过使用完全限定域名 (FQDN) 作为数据库位置来解决此问题; for example, server02.example.com instead of just server02 .例如, server02.example.com而不是server02 Since that doesn't look like a container name, it will cause Docker to forward the DNS request to your normal name server.由于这看起来不像容器名称,它会导致 Docker 将 DNS 请求转发到您的普通名称服务器。

The same issue bugged me for days and finally was able to solve it by following the steps below.同样的问题困扰了我好几天,终于能够按照以下步骤解决它。

In my case the SQL server was hosted on Azure managed instance and exposed a private endpoint.在我的例子中,SQL 服务器托管在 Azure 托管实例上,并暴露了一个私有端点。 The docker was able to resolve the DNS name of the Azure managed instance to a private IP address (as expected ). 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 also falls. 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也跌倒。 Hence, the docker assumed the SQL server to be hosted in the same local network and not outside (here enterprise network).因此,docker 假定 SQL 服务器托管在同一本地网络中,而不是外部(此处为企业网络)。 In my case the Windows VM I was working was already on the private network of the enterprise.就我而言,我正在工作的 Windows VM 已经在企业的专用网络上。

Solution:解决方案:

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>" . 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>" . Restart the docker desktop and the issue should be solved.重新启动 docker 桌面,问题应该得到解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从Linux Server上的Zend Framework连接外部MS SQL DB - Connect external MS SQL DB from Zend Framework on Linux Server 如何从ruby连接到MS Sql Server - How to connect from ruby to MS Sql Server 无法从 Docker 容器连接到外部 SQL 服务器 - Can't Connect to External SQL Server From Docker Container 无法从 SQL Server 数据库连接到 MS Power BI Desktop - Failed to connect from SQL Server database to MS Power BI Desktop 如何使用 php 将 opencart 应用程序连接到 ms sql server 数据库? - How to connect an opencart application to an ms sql server database using php? 无法从另一个 docker 容器连接到 MS SQL 服务器 docker 容器 - Couldn't connect to MS SQL Server docker container from another docker container Java连接到远程MS SQL Server数据库 - Java connect to a remote MS SQL server database 如何从另一个容器连接到 SQL 服务器 docker 容器? - How to connect to SQL Server docker container from another container? 无法通过 sql management studio 从 docker 容器连接到 ms sql 数据库 - Can't connect to ms sql database from docker container by sql management studio 如何连接到Docker容器内的SQL Server数据库? - How to connect to a SQL Server database inside a docker container?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM