Can't connect to docker sql server from NET Core 2.2 Web API Can't connect to docker sql server from NET Core 2.2 Web API docker docker

Can't connect to docker sql server from NET Core 2.2 Web API


The SQL Server instance is not on localhost. In the context of the web app, localhost is the container the web app is running in, which is not the same container that SQL Server is in.

When using Docker Compose, the service names end up as hostnames, which means that you can simply connect to sqldata,1433.

Adding the service name instead of the IP works, but whenever i migrate the database using "dotnet ef migrations add xyz" and "dotnet ef database update", i get an error. It says, server not found or not accessible.. So now i can connect, but cannot use dotnet ef.

The hostname is only valid within the vlan that your docker containers are running in, which is not the same vlan your desktop machine is in. To access the SQL Server container, you must either docker container exec into one of the containers and run the commands from there (where the sqldata hostname will exist) or you must use the IP address of the container, which you can get by running docker container ls to get the container id (guid) and then run docker inspect {container id}. The IP address will be in the NetworkSettings section of the resulting JSON.