System.Data.SqlClient.SqlException:与 ASP.NET Core 2.1 和 SQL Server 组合的网络相关或实例 docker
Posted
技术标签:
【中文标题】System.Data.SqlClient.SqlException:与 ASP.NET Core 2.1 和 SQL Server 组合的网络相关或实例 docker【英文标题】:System.Data.SqlClient.SqlException: A network-related or instance- docker compose with ASP.NET Core 2.1 and SQL Server 【发布时间】:2022-01-14 19:56:15 【问题描述】:Docker 编写
version: '3.4'
services:
legalregtech.web.host:
image: $DOCKER_REGISTRY-legalregtechwebhost
build:
context: .
dockerfile: src/LegalRegTech.Web.Host/Dockerfile
networks:
- dev-net
ports:
- "22742:80"
depends_on:
- sqlServer
sqlServer:
image: "mcr.microsoft.com/mssql/server:2019-latest"
environment:
ACCEPT_EULA: 'Y'
SA_PASSWORD: 'XXXXX'
ports:
- "1433:1433"
networks:
- dev-net
volumes:
- /C/Databases/SqlServer/Data:/var/opt/mssql/data
networks:
dev-net:
driver: bridge
ipam:
driver: default
config:
- subnet: 172.172.0.0/16
gateway: 172.172.0.1
Docker 文件:
FROM mcr.microsoft.com/dotnet/aspnet:2.1 AS base
WORKDIR /app
EXPOSE 80
FROM mcr.microsoft.com/dotnet/sdk:2.1 AS build
WORKDIR /src
COPY ["src/LegalRegTech.Web.Host/LegalRegTech.Web.Host.csproj", "src/LegalRegTech.Web.Host/"]
COPY ["src/LegalRegTech.Web.Core/LegalRegTech.Web.Core.csproj", "src/LegalRegTech.Web.Core/"]
COPY ["src/LegalRegTech.Application/LegalRegTech.Application.csproj", "src/LegalRegTech.Application/"]
COPY ["src/LegalRegTech.AzureCosmosDB/LegalRegTech.AzureCosmosDB/LegalRegTech.GraphDBConnector.csproj", "src/LegalRegTech.AzureCosmosDB/LegalRegTech.AzureCosmosDB/"]
COPY ["src/LegalRegTech.Core.Shared/LegalRegTech.Core.Shared.csproj", "src/LegalRegTech.Core.Shared/"]
COPY ["src/LegalRegTech.Core/LegalRegTech.Core.csproj", "src/LegalRegTech.Core/"]
COPY ["src/LegalRegTech.Application.Shared/LegalRegTech.Application.Shared.csproj", "src/LegalRegTech.Application.Shared/"]
COPY ["src/LegalRegTech.EntityFrameworkCore/LegalRegTech.EntityFrameworkCore.csproj", "src/LegalRegTech.EntityFrameworkCore/"]
RUN dotnet restore "src/LegalRegTech.Web.Host/LegalRegTech.Web.Host.csproj"
COPY . .
RUN dotnet publish "LegalRegTech.Web.Host.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=build /app/publish .
ENTRYPOINT ["dotnet", "LegalRegTech.Web.Host.dll"]
我得到了这个例外:
未处理的异常:System.Data.SqlClient.SqlException:建立与 SQL Server 的连接时发生与网络相关或特定于实例的错误。服务器未找到或无法访问。验证实例名称是否正确以及 SQL Server 是否配置为允许远程连接。 (提供者:TCP 提供者,错误:40 - 无法打开与 SQL Server 的连接)
在 System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity 标识、SqlConnectionString connectionOptions、SqlCredential 凭据、对象 providerInfo、String newPassword、SecureString newSecurePassword、Boolean redirectedUserInstance、SqlConnectionString userConnectionOptions、SessionData reconnectSessionData、Boolean applyTransientFaultHandling、String accessToken) 在 System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions 选项,DbConnectionPoolKey poolKey,对象 poolGroupProviderInfo,DbConnectionPool 池,DbConnection owningConnection,DbConnectionOptions userOptions) 在 System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool 池,DbConnection owningObject,DbConnectionOptions 选项,DbConnectionPoolKey poolKey,DbConnectionOptions userOptions) 在 System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject,DbConnectionOptions userOptions,DbConnectionInternal oldConnection) 在 System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject,DbConnectionOptions userOptions,DbConnectionInternal oldConnection) 在 System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject,UInt32 waitForMultipleObjectsTimeout,布尔 allowCreate,布尔 onlyOneCheckConnection,DbConnectionOptions userOptions,DbConnectionInternal& 连接) 在 System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject,TaskCompletionSource
1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection) at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource
1 重试,DbConnectionOptions userOptions,DbConnectionInternal oldConnection,DbConnectionInternal& 连接) 在 System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection externalConnection,DbConnectionFactory connectionFactory,TaskCompletionSource1 retry, DbConnectionOptions userOptions) at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource
1 重试) 在 System.Data.SqlClient.SqlConnection.Open() 在 Hangfire.SqlServer.SqlServerStorage.CreateAndOpenConnection() 在 Hangfire.SqlServer.SqlServerStorage.UseConnection[T](DbConnection dedicatedConnection, Func2 func) at Hangfire.SqlServer.SqlServerStorage.UseConnection(DbConnection dedicatedConnection, Action
1 action) 在 Hangfire.SqlServer.SqlServerStorage.Initialize() 在 /src/src/LegalRegTech.Web.Host/Startup/Startup.cs:line 149 中的 LegalRegTech.Web.Startup.Startup.ConfigureServices(IServiceCollection 服务)
SQL Server 运行良好,我可以在本地计算机上通过 SSMS 进行连接。我认为在容器中应用程序无法连接到 SQL Server。
当运行docker-compose up
时,会创建如下网络
它是使用后缀 6clicks_ 创建的,它是我的应用程序的根文件夹,当我在该网络上进行检查时,容器是
[
"Name": "6clicks_dev-net",
"Id": "b6f693ce2402e7c52d0c8af3048eaebd86ed19b41a43dca381f30712c00f4193",
"Created": "2021-12-10T02:34:23.8790393Z",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM":
"Driver": "default",
"Options": null,
"Config": [
"Subnet": "172.172.0.0/16"
]
,
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom":
"Network": ""
,
"ConfigOnly": false,
"Containers": ,
"Options": ,
"Labels":
"com.docker.compose.network": "dev-net",
"com.docker.compose.project": "6clicks",
"com.docker.compose.version": "2.0.0"
]
连接字符串:
"ConnectionStrings":
"Default": "Data Source=127.0.0.1; Initial Catalog=xxxxx;User ID=sa; Password=xxxxx; Persist Security Info=True; MultipleActiveResultSets=True"
我可以在 docker 日志上看到的原因是 Login failed for user 'sa'. Reason: Password did not match that for the login provided. [CLIENT: 172.172.0.2]
。但是我可以使用相同的密码使用 SSMS 登录
【问题讨论】:
您在尝试我的回答后是否看到有关密码不匹配的新错误消息?还是之前? 是的,更新后我遇到了同样的错误 我需要在某个地方启用安全性吗?防火墙什么的??不确定只是问 【参考方案1】:由于legalregtech.web.host
未配置为显式连接到sqlServer
,因此它会尝试按照appsettings.json
文件中的配置连接127.0.0.1
。
对于在 docker-compose 中使用,最简单的方法是通过添加环境变量来覆盖此连接字符串以访问 sqlServer
服务:
legalregtech.web.host:
# ...
environment:
- ConnectionStrings__Default=Data Source=sqlServer; Initial Catalog=xxxxx;User ID=sa; Password=xxxxx; Persist Security Info=True; MultipleActiveResultSets=True
(更紧凑的连接字符串是:Server=sqlServer;Database=xxxxx;User ID=sa;Password=xxxxx;MultipleActiveResultSets=True;
)
这是一个完整的 docker-compose 文件:
version: '3.4'
services:
mssql:
image: mcr.microsoft.com/mssql/server
environment:
- MSSQL_SA_PASSWORD=zHDgRw7qw4QJyrrhgXK2mG45
- ACCEPT_EULA=Y
- MSSQL_PID=Developer
dotnetapp:
image: dotnetapp
depends_on:
- mssql
build:
context: .
dockerfile: Dockerfile
restart: always
environment:
- ConnectionStrings__Default=Server=mssql;Database=amplily;User ID=sa;Password=zHDgRw7qw4QJyrrhgXK2mG45;MultipleActiveResultSets=True;
【讨论】:
仍然遇到同样的问题,没有解决问题 @SanJaisy 我已经用一个完整的例子更新了我的答案以上是关于System.Data.SqlClient.SqlException:与 ASP.NET Core 2.1 和 SQL Server 组合的网络相关或实例 docker的主要内容,如果未能解决你的问题,请参考以下文章