dotnet / dotnet/SqlClient

ef core7 , RemoteCertificateValidationCallback error was throwd in docker container

Open
#1,856 30 comments 6 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
989
Forks
340
Avg merge
4d 18h
Merged PRs (30d)
69

Description

## I try to run an asp.net core api with dotnet7 SDK in a docker container.

I have this connection string:
`server=xxx.xxx.xxx.xxx,SqlServerPort;database=DbName;user id=sa;password=DbPassword123;TrustServerCertificate=True;`

it **works fine on my local pc**. but when I run this project in a docker container throws this error:

> Microsoft.Data.SqlClient.SqlException (0x80131904): A connection was successfully established with the server, but then an error occurred during the pre-login handshake. (provider: TCP Provider, error: 35 - An internal exception was caught)
> System.Security.Authentication.AuthenticationException: The remote certificate was rejected by the provided RemoteCertificateValidationCallback.

**I use this Dockerfile:**
```dockerfile
FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
WORKDIR /src
COPY ["Project.Api/Project.Api.csproj", "Project.Api/"]
COPY ["Project.Module.Admin/Project.Module.Admin.csproj", "Project.Module.Admin/"]
COPY ["Project.Module.Shared/Project.Module.Shared.csproj", "Project.Module.Shared/"]
COPY ["Project.Data/Project.Data.csproj", "Project.Data/"]
COPY ["Project.Infrastructure/Project.Infrastructure.csproj", "Project.Infrastructure/"]
COPY ["Project.Identity/Project.Identity.csproj", "Project.Identity/"]
COPY ["Project.Job/Project.Job.csproj", "Project.Job/"]
COPY ["Project.Module.Driver/Project.Module.Driver.csproj", "Project.Module.Driver/"]
COPY ["Project.Module.User/Project.Module.User.csproj", "Project.Module.User/"]
RUN dotnet restore "Project.Api/Project.Api.csproj"
COPY . .
WORKDIR "/src/Project.Api"
RUN dotnet build "Project.Api.csproj" -c Release -o /app/build

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

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

**and use this docker-compose file:**

```docker-compose
version: "3.4"
services:
db-production:
image: "mcr.microsoft.com/mssql/server:2022-latest"
container_name: db-production
restart: always
environment:
ACCEPT_EULA: "Y"
MSSQL_SA_PASSWORD: "DbPassword123"
volumes:
- /home/volumes/mssql/data/production:/var/opt/mssql/data
ports:
- "1432:1433"
backend-production:
image: "backend_production:${TAG}"
container_name: backend-production
environment:
- "ASPNETCORE_ENVIRONMENT=Production"
build:
context: .
dockerfile: Project.Api/Dockerfile
ports:
- "${EXPOSED_PORT_PRODUCTION}:80"
restart: unless-stopped
depends_on:
- db-production
```

**I add my dbContext and SQL server ef core provider:**

```csharp
builder.Services.AddDbContextPool(options =>
{
options.UseSqlServer(builder.Configuration.GetConnectionString("ConnectionStringName"), ss =>
{
ss.EnableRetryOnFailure(3);
});
});
```

### if I downgrade EF Core package version to 6.0.1 its works fine on docker!

EF Core version: 7.0.0
Database provider: (e.g. Microsoft.EntityFrameworkCore.SqlServer)
Target framework: (e.g. .NET 7.0)
Operating system: Windows, Linux
IDE: (e.g. Jetbrains Rider 2022.3 EAP 9)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.