Asp.net core in docker Unhandled exception. SqlException (0x80131904): .., but then an error occurred during the pre-login handshake

1

I'm getting the error

"Unhandled exception. 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: 0 - Unknown error 203)"

when I start asp.net core application container using docker-compose in CI/CD flow from gitlab (self-hosted) runner on ubuntu 19.10 all docker images - latest.

After MSSQL starting i can normally connect to it using SSMS but application fails with the exception.

TargetFramework: netcoreapp3.1 ORM: EntityFrameworkCore 3.1.4

docker-compose.yml

version: '3.4'

services:
  websiteanalyzer:
    container_name: project-aspnet-core
    image: registry.gitlab.example.com/user/project/imagename:develop
    ports:
      - "34080:34080"
    volumes:
      - /data/project/app/conf/appsettings.json:/app/appsettings.json
    depends_on:
      - db
  db:
    container_name: project-db
    image: "mcr.microsoft.com/mssql/server"
    environment:
        SA_PASSWORD: "password"
        ACCEPT_EULA: "Y"
    ports:
      - "34433:1433"
    volumes:
      - /data/project/mssql/data:/var/opt/mssql/data
      - /data/project/mssql/log:/var/opt/mssql/log
      - /data/project/mssql/secrets:/var/opt/mssql/secrets

Dockerfile

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 AS base
WORKDIR /app
EXPOSE 80
#EXPOSE 443

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

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

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

appsettings.json

{
  "ConnectionStrings": {
    "DefaultConnection": "Server=db,34433;Database=master;User Id=sa;Password=password;MultipleActiveResultSets=true"
  },
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*"
}

Any idea what might have gone wrong here?

sql-server
asp.net-core
docker-compose
entity-framework-core
gitlab-ci
asked on Stack Overflow Jul 14, 2020 by Andy

1 Answer

0

Try appending this to your connection string in appsettings.json:

Integrated Security=false;TrustServerCertificate=true

Such that your config is:

    "DefaultConnection": "Server=db,34433;Database=master;User Id=sa;Password=password;MultipleActiveResultSets=true;Integrated Security=false;TrustServerCertificate=true"

And also use aspnet:3.1-bionic in your Dockerfile

FROM mcr.microsoft.com/dotnet/aspnet:3.1-bionic
answered on Stack Overflow Feb 6, 2021 by James K • edited Feb 6, 2021 by James K

User contributions licensed under CC BY-SA 3.0