2

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;"
  }
4
  • 1
    please try and replace the IP you've used to connect to the SQL server container with the service name or sqlserver Commented Oct 30, 2021 at 8:33
  • 1
    @NoamYizraeli , you mean something like that: "Server=sqlserver,1433;Database=myDb;User Id=sa;Password=StrongPassword;"? Commented Oct 30, 2021 at 8:46
  • if that configuration is in the .NET container then YES Commented Oct 30, 2021 at 8:48
  • 1
    When ranning 2 containers in same non default network (that's what happens when using docker compose file) they can reach each other by service name instead of ip Server=sqlserver,1433. Since you are exposing port 1433 you also can use Server=host.docker.internal,1433 Commented Oct 30, 2021 at 9:48

3 Answers 3

7

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
Sign up to request clarification or add additional context in comments.

3 Comments

Ok, the docker network inspect bridge tip was nice, I can "ping" the SQL Server container from the ASPNET container using it's internal ip address... But I can't do it using the NAME of the container. Is there something I need to do to make a container resolve other container's name?
@ikbalkazanc, you are right. in my case, the mistake was that i forgot to remove the container without removing the image. i did both and it worked.
A docker issue, using the user's solution worked for me when setting up the container, but when using Sql Server Management Studio (SSMS), I could only access the database using localhost, is there another way to create a label for the sql server container in the docker-compose file to use the configuration in both the appsetting.json and the SSMS credential? PS: Think of a problem I spent 2 weeks trying to solve. Thanks to user ikbalkazanc, here for me following his tip worked perfectly.
1

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

Comments

0

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.