How to reference host port from docker container in MSSQL Server connection string?

2

Orchestration:

  • Windows Host
  • My DB is running in a container on localhost:1444.
  • My API is running in a container on localhost:5000/5001.
  • They are both running on the default (bridge) network.
  • I can't use a custom network because currently our app references another API that is not in a container

At first I just left the connection string as .,1444 but realized that the container would have to look up outside of itself to get the host localhost it needed.

So far I've tried:

  • 127.0.0.1:1444
  • host.docker.internal:1444
  • 127.0.0.1,1444
  • host.docker.internal,1444
  • .,1444

I keep getting this error:

     Connection id "0HLV8EI9HSV9D", Request id "0HLV8EI9HSV9D:00000009": An unhandled exception was thrown by the application.
System.Data.SqlClient.SqlException (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: TCP Provider, error: 40 - Could not open a connection to SQL Server)```


windows
.net-core
network-programming
sql-server-2017
docker-desktop
asked on Stack Overflow Apr 24, 2020 by James Gooding • edited Apr 24, 2020 by James Gooding

1 Answer

1

The most common way to do this is to add --network="host" in your docker run command, then 127.0.0.1 in your docker container will point to your docker host.

If this is not suitable for you, this question contains a bunch of hacky ways to achieve the same thing.

Such as:

  • Mapping the local IP alias name (DNS) in the container
  • Volume mounting certain files for communication purposes
  • Enabling route_localnet for docker0 interface

I believe that link will help you come to a solution that is most suitable for you.

answered on Stack Overflow Apr 24, 2020 by Brand • edited Apr 24, 2020 by Brand

User contributions licensed under CC BY-SA 3.0