How to connect to SQL Sever docker container from another container? How to connect to SQL Sever docker container from another container? docker docker

How to connect to SQL Sever docker container from another container?


Docker has a DNS server built in, and containers connect to each other by the container name. In your case you named the SQL Server container mssql so that's the server name you need to put into your connection string for the .NET app: Server=mssql;Database=student;User Id=sa;Password=!Abcd123;.

Check out the .NET Core album viewer sample on GitHub, which uses Docker Compose to define a multi-container app.


I resolved the issue by inspecting container network firstly then find the network IP and put to appsetting.json

 "ConnectionStrings": {    "DefaultConnection": "Server=172.17.0.2;Database=VehicleKey;User Id=sa;Password=p@ssW0rd;"  }

inspect docker network

find name of SQLSERVERenter image description here


First of all, when you are using two or more containers, there couldn't be any localhost connections. Each docker container has own internal network IP.Once you started a container with an exposed port to be able to connect to that container you need to specify host IP (where container actually running).

So, as example, you should have next string for connection:

Server=192.168.1.99,1433;Database=student;User Id=sa;Password=!Abcd123;

where: 192.168.1.99 - actual IP of the host where docker container running.