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.