I have an ASP.NET Core WebAPI running inside a Windows Docker container. I'm having trouble connecting to an on premises SQL instance from inside the app.
This is the error message:
(0x80131904): 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) ---> System.ComponentModel.Win32Exception (53): The network path was not found
I found this issue on github, but it doesn't really apply to my situation.
I can make everything work by replacing the instance name with the server local IP address like so:
"ConnectionString_ThrowingError": "Data Source=MySqlInstance; (...)"
"ConnectionString_WorkingFine": "Data Source=192.168.0.123; (...)"
What is happening here? It seems .NET cannot resolve domain machine hostname.
Other details
- Using .Net Core 2.2
- Using Docker Windows container on Windows
- Tried hosting on both
Windows 10andWindows Server 2019. Both had the exact same error. - I can
pingthe SQL instance from inside the container using either its hostname or local IP. - Everything works when I replace the instance name with the local IP address in the connection string.
- Everything works normally if I run the app in the .NET CLI (not using Docker)
- The SQL instance I'm trying to reach is not hosted on Docker. It's hosted on another Windows Server on the same local network.
- I can reach others SQL instance hosted on Azure using
DataSource=MyAzureServer.database.windows.netwithout any issues.