6

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.

3
  • Did you resolve this? Commented Jan 3, 2018 at 2: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. Commented Jan 3, 2018 at 3: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. Commented Jan 4, 2018 at 4:39

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.