1

I'm having an issue trying to connect to my MSSQL database only with docker, I've already tried with IIS Express and its showing data correctly but when I try to get data from a request I get this error.

SqlException: 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)

I verified that TCP/IP is enable and also the 1433 port.Also im using this string connection

"TestConnection": "Server=localhost,1433;Database=BDREPOSITORIO;Integrated Security=True"

And my dockerfile is this

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

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

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

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

RUN sed -i 's/DEFAULT@SECLEVEL=2/DEFAULT@SECLEVEL=1/g' /etc/ssl/openssl.cnf
RUN sed -i 's/DEFAULT@SECLEVEL=2/DEFAULT@SECLEVEL=1/g' /usr/lib/ssl/openssl.cnf
Maytham Fahmi
  • 31,138
  • 14
  • 118
  • 137
LinoMacKay
  • 54
  • 8
  • 1
    please copy your docker line. in stead of using image. if you need to access your sql server from docker, you need to use IP address of you SQL server, not local host – Maytham Fahmi Jan 15 '21 at 17:01
  • I actually used 127.0.0.1 for connection and im getting the same result – LinoMacKay Jan 15 '21 at 17:15
  • not even that ip, I am talking about instance ip in LAN like 192.168.x.x as example. It should be external accessable. Imagine you need to connect 2 computers on lan. you do not use 127.0.0.1 – Maytham Fahmi Jan 15 '21 at 17:22
  • oh, now that i used another ip im getting another error something about Kerberos, at least i think is connectig to the database – LinoMacKay Jan 15 '21 at 17:27
  • yes, thanks for the help – LinoMacKay Jan 16 '21 at 04:20

1 Answers1

3

You have simply 2 issues with your connection string:

  1. Your first issue is you need to replace localhost with database (external) ip address. Both localhost and 127.0.0.1 are internal ip address.
  2. When number one is fixed, you get Kerberos issue, you can not use Integrated Security, you need to use database user name and password with the proper rights. Regarding this part check my answer.
Maytham Fahmi
  • 31,138
  • 14
  • 118
  • 137