【问题标题】:Trouble connecting from docker container with ASP.NET core to SQL Server container从带有 ASP.NET 核心的 docker 容器连接到 SQL Server 容器时出现问题
【发布时间】:2021-02-16 19:52:59
【问题描述】:

我有一个在 ASP.NET Core 中部署前端的容器,试图连接到后端 SQL Server 数据库。我正在使用 Docker 桌面 v19.03.13 运行 Windows 10。

网站容器建立在

FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build-env
WORKDIR /app

EXPOSE 80

# Copy csproj and restore as distinct layers
COPY ./FOOBAR/*.csproj ./
RUN dotnet restore

# Copy everything else and build
COPY . ./
RUN dotnet publish -c Release -o out

# Build runtime image
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "FOOBAR.dll"]

数据库建立在

FROM mcr.microsoft.com/mssql/server:2017-latest
USER root
COPY setup.sql setup.sql
COPY import-data.sh import-data.sh
COPY entrypoint.sh entrypoint.sh
RUN chmod +x entrypoint.sh
CMD /bin/bash ./entrypoint.sh

在 docker、.NET、Python、SQL Server Management Studio 之外运行时,一切都非常出色。

在 .NET 中,我的连接字符串是:

Server=localhost;Database=FOOBAR;Integrated Security=True

在 Python 中:

DRIVER={ODBC Driver 17 for SQL Server};server=localhost;database=FOOBAR;Trusted_Connection=yes;

所以我需要将它部署到没有域控制器的网络,所以我需要处理所有数据库身份验证。

当我构建容器时,我将 .NET 连接字符串更改为

dbConnection="Server=host.docker.internal;Database=FOOBAR;User Id=sa;Password=Password1!;"

我用

生成我的容器
docker run -e ACCEPT_EULA=Y -e SA_PASSWORD=Password1! -p 1433:1433 -v c:\temp\:/var/opt/mssql/data --name foobar_db -d foobar_db:1.0
docker run -p 8080:80 --name foobar --link foobar_db:foobar_db -d foobar:1.0

我的容器启动了,我的数据库部署得很好。在主机上,我可以使用 SQL Server Management Studio 和 Python,并使用上面的凭据连接到我的数据库容器,并完美地连接和执行读/写。

当我使用 .NET 连接时

Server=host.docker.internal;Database=FOOBAR;User Id=sa;Password=Password1!;

我可以看到我的 SQL Server 容器抱怨登录无效,

用户“6794cfd81d48\Guest”登录失败

我可以确认6794cfd81d48 是我的SQL Server 容器foobar_db 的哈希值。

IIS 可以正常提供网页,但问题在于连接到数据库。即使我提供了正确的用户名和密码,我也无法从另一个容器连接到 SQL Server 容器,因为它认为我是该容器的来宾。根据部署环境,通常我会为机器或用户创建 SQL Server 登录,但在这种情况下不会。

【问题讨论】:

  • Trusted_connectionusername/password 互斥,你要哪一个?如果您想以sa 登录,您需要trusted_connection=no。也就是说trusted_connection是指使用Kerberos/NTLM进行认证,而不是用户名/密码组合
  • 对不起。我在上面编辑。我忘了提到我们开发了所有服务,现在我们被告知将其部署到一个没有 Windows 域控制器的小飞地,因此我们必须处理所有身份验证,直到系统更加成熟和过去的概念阶段。

标签: sql-server docker


【解决方案1】:

有一个违规的 IT 安全软件应用程序被确定为原因。 IT 设置了一个 passthrough,以便 docker 应用程序可以通过安全性和一切按设计工作。

【讨论】:

    猜你喜欢
    • 2022-08-16
    • 2021-12-15
    • 1970-01-01
    • 2021-09-22
    • 1970-01-01
    • 2019-08-23
    • 1970-01-01
    • 2019-05-29
    • 2016-12-26
    相关资源
    最近更新 更多