How do I connect the local SQL Server database for the ASP.NET Core application running inside either local Docker or Kubernetes?

1/7/2019

I created an ASP.NET Core Web API using VS2017. After that I enabled the Docker support for my application.

Next, I was implemented the EF Core feature. After that I tested application locally then it’s working fine, database also created. But whenever I run the application inside local Docker or local Kubernetes the application won't work properly as I expected. because I used the Local SQL Server so, whatever the container running inside either Docker or Kubernetes it doesn't know the SQL Server or SQL Server database.

Can anyone suggest how to use the local database in the container running inside either Docker or Kubernetes?

-- Pradeep
asp.net-core-2.0
docker
kubernetes
sql-server

1 Answer

1/7/2019

You need to give the host's IP. In linux, you can use "host.docker.internal" hostname to connect to the host machine. It is supposedly working in Windows, however it has many, many, far too many problems in Windows.

If this hostname does not work for you, you have 2 IP addresses. One is the docker's gateway, that should start with 10...* or 172...* depending on how you set it up. Normally to learn this one, use docker inspect <container> and you can see the default gateway in the network section. However, Kubernetes might change these and it might be providing a better means to access the host. I did not use Kubernetes, so I don't know.

The other option is to use the IP address of the host, assigned by your network using DHCP. It should normally start with 192.168...

Your containers should be able to access applications on your host using these IP addresses. If the problem persists, turn off your firewall, and try pinging from inside the containers.

-- Yamaç Kurtuluş
Source: StackOverflow