5

A .NET application in a Docker container (based on microsoft/dotnet-framework image) fails to connect to SQL Server 2016 installed on the Docker host system. The Docker host is Windows Server 2016. Docker version is 17.03.2-ee-5.

I run the container and .NET application using the command sequence:

docker run -it microsoft/dotnet-framework cmd
docker cp App <container-id>:/
docker exec -it <container-id> cmd
cd App
TestConn.exe

TestConn.exe throws an exception after ~10 seconds, complaining that it cannot connect to SQL Server. The connection string is:

Data Source=localhost;Initial Catalog=SomeDB;Persist Security Info=False;User ID=appuser;Password=apppwd;MultipleActiveResultSets=False;Connection Timeout=30;

If I run TestConn.exe in the Docker host system, the application connects to SQL Server successfully.

I added --expose=1433 to the docker run command - did not work. The way I expect this to work is that TestConn.exe attempts a connection to localhost (default SQL port 1433), which in turn connects to the port 1433 in the Docker host, which corresponds to SQL Server.

Web User
  • 6,444
  • 12
  • 47
  • 82
  • Did you resolve this? – Andrew Harris Jan 03 '18 at 02:16
  • @AndrewHarris I did, sort of. I got the IP address of the Docker host (which is itself a VirtualBox VM), added `-p 1433:1433` to the docker run command and altered the connection string, replacing `localhost` with the IP address of the host. _This allowed TestConn.exe to connect to SQL Server in the Docker host_. The reason I have kept the question open is because I manually determined that IP address and added it to the connection string. I was hoping for some feature of Docker that would allow the client to know the host's IP address. – Web User Jan 03 '18 at 03:32
  • 2
    Thanks for the reply, I actually sorted mine this morning. Turns out the windows firewall on the host was blocking connections from the container. FWIW the -p maps from host to container, not the other way around. Ive yet to find a solution for localhost on container mapping to localhost on the host. – Andrew Harris Jan 04 '18 at 04:39

1 Answers1

4

The feature you are looking for does exist, the ip is "host.docker.internal", you can substitute "localhost" by that and the app will be able to connect to the DB running in another of your docker containers.

This is available only from version 18.03 onwards and for Mac and Windows (no support for Linux on this one yet...). In Linux "Use --net="host" in your docker run command, then localhost in your docker container will point to your docker host."

How to access host port from docker container has more information on this.

eat chocolate
  • 121
  • 1
  • 9
  • Interestingly, EntityFramework seems to be doing something behind the scenes with this to be able to use localhost, when switching to `SqlConnection` I had to use `host.docker.internal` which is not a bad thing i would like to add - just strange didn't have to do it for EF! This post and answer was a massive time saver! – morleyc Jul 11 '20 at 13:57