简体   繁体   English

如何从 .net Core Linux Docker 容器(使用 Docker for Windows)连接到 Internet 上的 Sql Server?

[英]How can I connect to a Sql Server on the internet from a .net Core Linux Docker container (using Docker for Windows)?

We're trying to access a Sql Server database that's on a remote network from within a Docker container.我们正在尝试从 Docker 容器中访问远程网络上的 Sql Server 数据库。 However, every attempt at connecting just hangs the application (no exception, no timeout, just endless nothing).但是,每次连接尝试都会挂起应用程序(没有例外,没有超时,只是无休止地什么都没有)。

It's easy to reproduce by creating a new .Net Core 3.1 console application and adding this code (add package reference to Microsoft.Data.SqlClient):通过创建新的 .Net Core 3.1 控制台应用程序并添加此代码(添加对 Microsoft.Data.SqlClient 的包引用)可以轻松重现:

using System;
using System.Threading.Tasks;
using Microsoft.Data.SqlClient;

namespace ConsoleApp1
{
    class Program
    {
        static async Task Main(string[] args)
        {
            var connStr = "Data Source=<public server ip>,1433;Integrated Security=False;Initial Catalog=TestDatabase;User ID=user;Password=password";
            await using var connection = new SqlConnection(connStr);

            await connection.OpenAsync();

            Console.WriteLine("Hello World!");
        }
    }
}

Then add Docker support via Visual Studio, which generates a DOCKERFILE:然后通过 Visual Studio 添加 Docker 支持,它会生成一个 DOCKERFILE:

#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.

FROM mcr.microsoft.com/dotnet/core/runtime:3.1-buster-slim AS base
WORKDIR /app

FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
WORKDIR /src
COPY ["ConsoleApp1/ConsoleApp1.csproj", "ConsoleApp1/"]
RUN dotnet restore "ConsoleApp1/ConsoleApp1.csproj"
COPY . .
WORKDIR "/src/ConsoleApp1"
RUN dotnet build "ConsoleApp1.csproj" -c Release -o /app/build

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

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

Now when we start the project as the ConsoleApp1 -profile the connection is made as expected.现在,当我们以ConsoleApp1 -profile 启动项目时,连接按预期进行。 When starting the project using the Docker -profile the application just hangs on the await connection.OpenAsync();当使用Docker启动项目时,应用程序只是挂在await connection.OpenAsync();await connection.OpenAsync(); call.称呼。

There's no exception or anything.没有例外或任何事情。 It just doesn't connect and just sits there indefinitely...它只是没有连接,只是无限期地坐在那里......

I realise running a Linux container on a Windows machine involves all kinds of magic, but creating a webrequest to an external page does work.我意识到在 Windows 机器上运行 Linux 容器涉及各种魔法,但创建一个对外部页面的 webrequest 确实有效。 Which gives me the impression that it has something to do with the port number or the nature of a connection to a Sql Server.这给我的印象是它与端口号或与 Sql Server 连接的性质有关。

Any help would be greatly appreciated.任何帮助将不胜感激。

As it turns out, the new version of the mcr.microsoft.com/dotnet/core/runtime:3.1-buster-slim source-image is much stricter on the TLS/Cipher usage.事实证明,新版本的mcr.microsoft.com/dotnet/core/runtime:3.1-buster-slim源图像对 TLS/Cipher 的使用更加严格。

This, combined with a bug in the SqlClient caused this behaviour.这与 SqlClient 中的错误相结合导致了这种行为。 As it turns out the issue and various resolutions/workarounds is very extensively discussed on the SqlClient repository:事实证明,在 SqlClient 存储库上非常广泛地讨论了该问题和各种解决方案/解决方法:

https://github.com/dotnet/SqlClient/issues/201 https://github.com/dotnet/SqlClient/issues/201

暂无
暂无

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

相关问题 Docker容器中的.Net Core应用无法连接到SQL Server - .Net Core app in docker container can't connect to SQL Server 无法从带有 ASP.NET Core 的 Docker 容器连接到 SQL Server 容器 - Can't connect from the Docker container with ASP.NET Core to SQL Server container 如何从 Linux Docker 容器连接到本地 SQL Server - How to Connect to Local SQL Server From Linux Docker Container 无法从 Docker (Linux) 中运行的 ASP.NET Core 连接到 SQL Server 命名实例 - Can't connect to SQL Server named instance from ASP.NET Core running in Docker (Linux) 如何从 windows Z05B6053C415A2134EAD6 容器中的 .NET 应用程序连接到在主机上运行的 SQL 服务器 - How do you connect to SQL Server running on the host from a .NET app in a windows docker container 为什么无法将我的 .net core 容器连接到 SQL Server docker 容器? - Why can't connect my .net core container to the SQL Server docker container? SQL Server Docker 容器可以在 Windows Server core 2022 上运行吗(“linux”不能在这个平台上使用) - Can a SQL Server Docker container run on Windows Server core 2022 ("linux" cannot be used on this platform) .NET Core 3.0 Docker 容器无法连接到 SQL 服务器 - .NET Core 3.0 Docker Container Won't Connect to SQL Server 从 asp.net 核心容器连接到 Docker SQL 服务器容器 - Connect to Docker SQL Server container from asp.net core container 无法从 Docker 容器(Linux 映像)连接到 SQL Server - Unable to connect to SQL Server from Docker container (Linux image)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM