如何连接在本地Docker或Kubernetes中运行的ASP.NET Core应用程序的本地SQL Server数据库? [英] How do I connect the local SQL Server database for the ASP.NET Core application running inside either local Docker or Kubernetes?

查看:85
本文介绍了如何连接在本地Docker或Kubernetes中运行的ASP.NET Core应用程序的本地SQL Server数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用VS2017创建了ASP.NET Core Web API.之后,我为我的应用程序启用了Docker支持.

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

接下来,我实现了EF Core功能.之后,我在本地测试了应用程序,然后运行正常,还创建了数据库.但是,每当我在本地Docker或本地Kubernetes中运行应用程序时,该应用程序将无法正常运行.因为我使用了本地SQL Server,所以无论在Docker或Kubernetes中运行的容器是什么,都不知道SQL Server或SQL Server数据库.

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.

有人可以建议如何在Docker或Kubernetes内部运行的容器中使用本地数据库吗?

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

推荐答案

您需要提供主机的IP.在Linux中,您可以使用"host.docker.internal"主机名连接到主机.它据说可以在Windows中运行,但是在Windows中却有很多很多太多的问题.

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.

如果该主机名不适合您,则您有2个IP地址.一个是码头工人的网关,应以10. ..*或172. ..*开头,具体取决于您的设置方式.通常,要学习这一点,请使用 docker inspect< container> ,您可以在网络部分中看到默认网关.但是,Kubernetes可能会更改这些设置,它可能提供了一种更好的访问主机的方法.我没有使用Kubernetes,所以我不知道.

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.

另一种选择是使用网络使用DHCP分配的主机的IP地址.它通常应以192.168..开头.

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...

您的容器应该能够使用这些IP地址访问主机上的应用程序.如果问题仍然存在,请关闭防火墙,然后尝试从容器内部ping通.

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.

这篇关于如何连接在本地Docker或Kubernetes中运行的ASP.NET Core应用程序的本地SQL Server数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆