Home » How To » How to Connect your Local SQL Server from Docker

How to Connect your Local SQL Server from Docker

If you are running an ASP.NET Core application from Docker and connecting to a SQL Server running on your PC then you might find you can’t connect to it. In this post we’ll look at what you need to do to make sure your containerized app can access SQL Server hosted on your own PC.

SQL Server can be connected to over TCP/IP

Make sure you can connect to your SQL Server instance via your machine’s IP. To test this use SQL Server Management Studio and try to connect to it with the IP address of your machine. If you get a network related error then this is probably not enabled.

Open SQL Server Configuration Manager and make sure TCP/IP is enabled.

SQLServerOverTcpIpEnabled.PNG

Then make sure SQL Server is listening on all IP addresses:

SQLServerOverTcpIpListenAll.PNG

Connection String Uses Host IP Address

In a container, localhost refers to the container, not the host machine, so we can’t use (local) in our connection string. We must put the IP address of the host. Also we must put the port number. By default that will be 1433 but you can confirm that from SQL Server Configuration Manager.

Related:  Top 50 SQL Server Interview Questions and Answers

An example connection string would be: 

Server=192.168.1.36,1433;Database=MyTechMintDb;User Id=MyUser;Password=MyStringPassword;MultipleActiveResultSets=True

Leave a Comment