Windows container disallows SQL Server connection to outside

  1. I have an ASP-NET core windows container on my docker desktop for Windows. The dockerfile and docker-compose file are created by Visual Studio by default.

Dockerfile:

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

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY [“AspNetCore1/AspNetCore1.csproj”, “AspNetCore1/”]
RUN dotnet restore “AspNetCore1/AspNetCore1.csproj”
COPY . .
WORKDIR “/src/AspNetCore1”
RUN dotnet build “AspNetCore1.csproj” -c Release -o /app/build

FROM build AS publish
RUN dotnet publish “AspNetCore1.csproj” -c Release -o /app/publish /p:UseAppHost=false

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .

ENTRYPOINT [“dotnet”, “AspNetCore1.dll”]

docker-compose.yml:

version: ‘3.4’

services:
aspnetcore1:
image: ${DOCKER_REGISTRY-}aspnetcore1
build:
context: .
dockerfile: AspNetCore1\Dockerfile

  1. I can ping outside websites from within the container, so the container’s networking including its DNS is fine.

  2. My container’s public IP address is the same as my host PC. When I ran the ASP-NET core app on my host PC, it can connect to and query the AWS RDS database without any problem. So the AWS database and its accessibility such as security groups etc. is not the problem.

  3. However, when I ran the asp-net core app in the container, it can’t connect to the AWS RDS database:

Microsoft.Data.SqlClient.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: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)’

It appears to me that the container itself specifically disallows SQL Server connections. Please help!

From inside the container there wouldn’t be any rules to deny traffic, so I’d double check the networking connectivity - container IP address, username, password from inside the container.