Connect to the SQL Server database from a docker container


I have docker for windows installed on my machine. There is a console application targeting .net core 1.0.0 that tries to access a SQL Server database running on a different VM. I can ping the VM running SQL Server from my machine.

When I try to run the console application using dotnet run from the command prompt on my machine it works fine. But when the same application is run inside a docker container I get a message

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)

I tried using

docker run --add-host sqldemo:<VM running sql server ip here>

but that made no difference.

You can run a docker container with network settings set to host. Such a container will share the network stack with the docker host and from the container point of view, localhost (or will refer to the docker host.

docker run --net=host ...

Then you should get the SQL Server database from inside the docker container as you do from your host.