During development we might want to connect from docker instance to SQL Server running in host. SQLServer uses port 1433 for default instance. But named instance will be using a dynamic port instead of standard 1433 port.
To connect from container instance to host machine, use the network name host.docker.internal
Step 1. Find the Dynamic port used.
Go to SQL Server configuration manager.
Note the Tcp dynamic ports under IP All.
Tip: SQL Configuration manager is now available as an mmc add-in. Refer https://blog.sqlauthority.com/2019/03/01/sql-server-sql-server-configuration-manager-missing-from-start-menu/
Step 2:
Add an inbound rule in firewall to allow connections from this port.
Step 3:
Your connection string will be in the following format
server=host.docker.internal\\SQL2017,55215; database=testdb;Max Pool Size=100;uid=sa;pwd=password;
Here SQL2017 is the instance name and 55215 is the dynamic port used by named instance.
Shalom and happy coding!

