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.
-p 1433:1433to the docker run command and altered the connection string, replacinglocalhostwith 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.