0

I have trying to dockerize my a ASP.Net Core 2.2 web application by running on Linux Docker image. I am however getting the following error when it is trying to connect to SQL server running on local windows machine. Some of the things that I have tried are:

  • enabling TCP\IP in SQL Server Network configuration, the Listen All is set to yes, IPAddress=> IPAll=>TCP Port=49172
  • turning off windows firewall on public network.
  • Changing my SQL server connection string to use my IPv4 address eg: instead of DataSource=DESKTOP-2LS7578\\SQLEXPRESS use DataSource=192.168.18.65\\SQLEXPRESS
  • Added rule in Windows firewall to allow inbound traffic from port 49172, changed my SQL server connection string to use the IP Address listed under DockerNAT and port 49172. eg: instead of DataSource=DESKTOP-2LS7578\\SQLEXPRESS use DataSource=<IP Address listed under NAT>\\SQLEXPRESS:49172
  • ensured that my sql server browser service is running.

Update 2(using Instructions from here):

  • changed my SQL server connection string to use the port listed under SQL Server Network configuration=> TCP\IP => IPAddress=> IPAll=>TCP Dynamic Port so it becomes "host.docker.internal\SQLEXPRESS,"
  • Enable firewall for the port you've found

What else can I try?

StackTrace:

An unhandled exception occurred while processing the request.
SocketException: Success
Microsoft.Data.SqlClient.SNI.SSRP.GetPortByInstanceName(string browserHostName, string instanceName)

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: 25 - Connection string is not valid)

Microsoft.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, object providerInfo, string newPassword, SecureString newSecurePassword, bool redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, bool applyTransientFaultHandling, string accessToken)

Dockerfile:

FROM mcr.microsoft.com/dotnet/core/aspnet:2.2-stretch-slim AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/core/sdk:2.2-stretch AS build
WORKDIR /src
COPY ["Scrubber/Scrubber.csproj", "Scrubber/"]
COPY ["SimplerProducts.MicrosoftEntityFrameworkCoreStorage/SimplerProducts.MicrosoftEntityFrameworkCoreStorage.csproj", "SimplerProducts.MicrosoftEntityFrameworkCoreStorage/"]
RUN dotnet restore "Scrubber/Scrubber.csproj"
COPY . .
WORKDIR "/src/Scrubber"
RUN dotnet build "Scrubber.csproj" -c Release -o /app

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

FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "Scrubber.dll"]
Ajit Goel
  • 4,180
  • 7
  • 59
  • 107
  • Can you provide your dockerfile and docker run command. – mchawre Jun 26 '19 at 04:33
  • @mchawre, I have added my dockerfile. with respect to the docker run command, I am running my application in docker using visual studio 2019, using the run button. – Ajit Goel Jun 26 '19 at 04:52
  • 2
    did you try to use `network: host` in your docker config ? I think it the way to make this work – LinPy Jun 26 '19 at 06:17
  • Try connectionstring like `"DefaultConnection": "Data Source=host.docker.internal\\SQLEXPRESS,1433;Initial Catalog=TestDockerSQL;User ID=xx;Password=xx;"` – Edward Jun 26 '19 at 10:01
  • @TaoZhou, I changed the connection string to use host.docker.internal, use port 1433, connect using sql server authentication(instead of windows authentication), allow port 1433 as a inbound rule in windows firewall, . I now get a "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". Any inputs on what else I can try? – Ajit Goel Jun 29 '19 at 05:38

1 Answers1

0

I was able to get the application working by not specifiying the instance name in the connection string and using SQL server authentication. i.e change the connection string from Data Source=DESKTOP-2LS7578\\SQLEXPRESS;Integrated Security=True; to Data Source=host.docker.internal,1433;User ID=<UserID>;Password=<Password>;

Ajit Goel
  • 4,180
  • 7
  • 59
  • 107