[英]How do I connect the local SQL Server database for the ASP.NET Core application running inside either local Docker or Kubernetes?
I created an ASP.NET Core Web API using VS2017.我使用 VS2017 创建了一个 ASP.NET Core Web API。 After that I enabled the Docker support for my application.之后,我为我的应用程序启用了 Docker 支持。
Next, I was implemented the EF Core feature.接下来,我实现了 EF Core 功能。 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.但是每当我在本地 Docker 或本地 Kubernetes 中运行应用程序时,应用程序都不会像我预期的那样正常工作。 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.因为我使用了本地 SQL Server,所以无论容器在 Docker 还是 Kubernetes 中运行,它都不知道 SQL Server 或 SQL Server 数据库。
Can anyone suggest how to use the local database in the container running inside either Docker or Kubernetes?谁能建议如何在 Docker 或 Kubernetes 中运行的容器中使用本地数据库?
You need to give the host's IP.您需要提供主机的IP。 In linux, you can use "host.docker.internal" hostname to connect to the host machine.在 linux 中,您可以使用“host.docker.internal”主机名连接到主机。 It is supposedly working in Windows, however it has many, many, far too many problems in Windows.据说它可以在 Windows 中运行,但是它在 Windows 中存在很多很多问题。
If this hostname does not work for you, you have 2 IP addresses.如果此主机名不适合您,则您有 2 个 IP 地址。 One is the docker's gateway, that should start with 10. .一个是 docker 的网关,应该从 10. . .* or 172. . .* 或 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.通常要学习这个,使用docker inspect <container>
,您可以在网络部分看到默认网关。 However, Kubernetes might change these and it might be providing a better means to access the host.然而,Kubernetes 可能会改变这些,它可能会提供一种更好的方式来访问主机。 I did not use Kubernetes, so I don't know.我没有使用Kubernetes,所以我不知道。
The other option is to use the IP address of the host, assigned by your network using DHCP.另一种选择是使用主机的 IP 地址,由您的网络使用 DHCP 分配。 It should normally start with 192.168.它通常应以 192.168 开头。 . . . .
Your containers should be able to access applications on your host using these IP addresses.您的容器应该能够使用这些 IP 地址访问主机上的应用程序。 If the problem persists, turn off your firewall, and try pinging from inside the containers.如果问题仍然存在,请关闭防火墙,然后尝试从容器内部 ping。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.