Docker Community Forums

Share and learn in the Docker community.

Application running on Linux Docker image, is unable to connect to SQL server on local windows machine

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

I have received a comment to use network: host in my docker config but I am unsure how I need to change my Dockerfile(?) to make that change.

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)

Dockerfile:

WORKDIR /app
EXPOSE 80
EXPOSE 443

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”]