Home > Blockchain >  Why connection to docker aspnet api mssql db connetion failed (Could not open a connection to SQL
Why connection to docker aspnet api mssql db connetion failed (Could not open a connection to SQL

Time:08-25

may be somebody know what the problem appears. I try to start api with db conteiners but always my Fluent Migrator failed with db connection

I created the docker file:

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build-env
WORKDIR /src

COPY *.sln "./"
COPY ["Desk.API/Desk.API.csproj", "Desk.API/"]
COPY ["Desk.Migrations/Desk.Migrations.csproj", "Desk.Migrations/"]
RUN dotnet restore "Desk.API/Desk.API.csproj"

COPY . .
WORKDIR "/src/Desk.API"
RUN dotnet publish "Desk.API.csproj" -c Release -o /app --no-restore

FROM mcr.microsoft.com/dotnet/aspnet:6.0
WORKDIR /app
COPY --from=build-env /app .

EXPOSE 80

ENTRYPOINT ["dotnet", "Desk.API.dll"]

and the docker-compose file:

version: '3.9'

services:
  sqldata:
    image: mcr.microsoft.com/mssql/server:2022-latest
    environment:
      - SA_PASSWORD=vV5r9tn0M4@
      - ACCEPT_EULA=Y
    ports:
      - "1450:1433"

  api:
    build:
      context: .
      dockerfile: Desk.API/docker/Dockerfile
    ports:
     - "8080:80"
    depends_on:
      - sqldata

connection string

"ConnectionStrings": {
    "Default": "Data Source=sqldata;Initial Catalog=Desk;Persist Security Info=True;User ID=sa;Password=vV5r9tn0M4@"
},

I always get the next exception

fail: FluentMigrator.Runner.Processors.SqlServer.SqlServer2016Processor[0]       There was an exception checking if table VersionInfo in (null) exists       Microsoft.Data.SqlClient.SqlException (0x80131904): A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: TCP Provider, error: 40 - Could not open a connection to SQL Server: Could not open a connection to SQL Server)

enter image description here

What am I doing wrong? I don't understand how fix this issue to provide correct connection

CodePudding user response:

You are using the default bridge network and you can resolve hostname in default bridge network.

Containers on the default bridge network can only access each other by IP addresses, unless you use the --link option, which is considered legacy

You can define your own network to bypass the issue

version: '3.9'

services:
  sqldata:
    image: mcr.microsoft.com/mssql/server:2022-latest
    environment:
      - SA_PASSWORD=vV5r9tn0M4@
      - ACCEPT_EULA=Y
    ports:
      - "1450:1433"
    networks:
      - gateway
  api:
    build:
      context: .
      dockerfile: Desk.API/docker/Dockerfile
    ports:
     - "8080:80"
    depends_on:
      - sqldata
    networks:
      - gateway
networks:
  gateway: {}

Also try removing Persist Security Info=True from connection string

CodePudding user response:

as the mssql default port is 1433 and the both services are on the same network (@Hans Kilian => so the first connection string should works). If the problem persist i suggest you to :

  • test the connection to the mssql server using the @D-Shih configuration on port 1450 (on the host) with a client (like DBeaver)
  • use your first connection (the one without the port) string on the api service
  • log (if not already) the connection string before starting the application to check it's the one you want to use ?
  • Related