12

I've built a simple api using asp.net core that returns some data from a sql server database.

It runs fine in VS and from the command line, but when i build and run the app as a docker container on my windows 10 machine when i call the api i keep getting this error

System.Data.SqlClient.SqlException: Connection Timeout Expired. The timeout period elapsed during the post-login phase. The connection could have timed out while waiting for server to complete the login process and respond; Or it could have timed out while attempting to create multiple active connections. The duration spent while attempting to connect to this server was - [Pre-Login] initialization=425; handshake=265; [Login] initialization=5; authentication=9; [Post-Login] complete=14034; ---> System.ComponentModel.Win32Exception: Unknown error 258

I'm not really sure what this is telling me, it's as if it cant find the sql server machine. Do i have to expose port 1433 somehow in the docker config or when i run the container up?

Marco Salerno
  • 5,131
  • 2
  • 12
  • 32
beakersoft
  • 2,316
  • 6
  • 30
  • 40
  • Docker containers can't connect to it's host by default and containers only communicate in an internal virtual network. So you'll have to put the host where the database runs in that network and use this docker network IP (not the server IP from your DHCP) to connect to your networked devices. See https://docs.docker.com/engine/userguide/networking/ / http://stackoverflow.com/a/24326540/455493 – Tseng Oct 18 '16 at 21:34
  • 1
    hi bro, did you solve this problem?I met same problem, i think may be the app on docker don't support the mssql - win – James.YinG May 18 '17 at 08:36
  • I've added a bounty to this question. Specifically, I'm trying to connect to an external SQL Server from my linux ASP.NET Core Docker container. – Muhammad Rehan Saeed Aug 31 '17 at 16:07
  • @beakersoft I think you also mean to connect to an external SQL Server (SQL Server on Linux was not out when you asked this question), can you please clarify that in your question or post your solution if you solved this problem. – Muhammad Rehan Saeed Aug 31 '17 at 16:08

2 Answers2

4

As Tseng explained in his comment, your containers are in their own network, not in your host's network. You'll want indeed to expose that port (or map to another available port on your host), which you can easily do with Docker Compose. In the example below (that you'd but in a docker-compose.yml file), sql is the name of your database container, 1337 is the local port, and 1433 is the container's port.

sql:
 ports:
 - "1337:1433"
otravers
  • 1,321
  • 8
  • 16
1

Not sure if my case was the same as your case, but in my case I was connecting from a docker container to a sql server 2008 R2 running on the host (real pc). I had to install SP3 for sql server 2008 R2 to fix it.

More info here

xhafan
  • 2,140
  • 1
  • 26
  • 26