skip to Main Content

I have a container deploying a WEB API in ASP.NET Core trying to connect to the SQL Server database. I am running Windows 10 with Docker Desktop.

I can successfully connect to the Docker container with SQL Server from SQL Server Management Studio and my ASP.NET Core app (without container).

But when I run my ASP.NET Core inside the container, I’ve got an error:

fail: Microsoft.AspNetCore.Server.Kestrel[13]

      Connection id "0HMCRFGHIEO1Q", Request id "0HMCRFGHIEO1Q:00000002": An unhandled exception was thrown by the application.

      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)

         at Microsoft.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
...

docker-compose for the SQL Server:

sqlserver:
        image: mcr.microsoft.com/mssql/server:2019-latest
        ports:
            - 1433:1433
        volumes:
            - my-volume:/var/opt/mssql
        environment:
            SA_PASSWORD: "StrongPassword"
            ACCEPT_EULA: "Y"

docker-compose for the WEB API:

web_api:
        build:
            dockerfile: WebApi/Dockerfile
        ports:
            - 5000:80
        depends_on:
            - sqlserver

Dockerfile for the WEB API:

FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base
WORKDIR /app
EXPOSE 80

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

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

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

The connection string to SQL Server:

"ConnectionStrings": {
    "SqlConnectionString": "Server=192.168.0.108,1433;Database=myDb;User Id=sa;Password=StrongPassword;"
  }

3

Answers


  1. Chosen as BEST ANSWER

    Sorry, It was my fail:(
    I just forgot that docker-compose up -d command doesn't rebuild Docker images.
    I removed all images, and run the command again.
    With the configurations above it works fine for me!


  2. By default, containers are connected to the bridge driver. To access sqlserver from ASP.NET, you must use the container name of the sqlserver in connection string. Like this.

    "ConnectionStrings": {
        "SqlConnectionString": "Server=sqlserver,1433;Database=myDb;User Id=sa;Password=StrongPassword;"
    }
    

    For more network details you can run the cmd command below.

    docker network inspect bridge
    
    Login or Signup to reply.
  3. command for inspect bridge helped a lot:
    docker network inspect bridge

    i have Windows development machine to run Visual studio and docker desktop on it. so i tried to connect from one container with asp.net core app to another with ms sql running

    i used host.docker.internal as Data source in my connection string and it worked fine

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search