LinoMacKay
LinoMacKay

Reputation: 64

Can't connect to SQL Server database via docker

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

Upvotes: 1

Views: 489

Answers (1)

Maytham Fahmi
Maytham Fahmi

Reputation: 33377

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.

Upvotes: 3

Related Questions