简体   繁体   English

如何为在本地 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?

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.

相关问题 如何从在容器中运行的 ASP.NET 核心应用程序连接到具有集成安全性的 Windows 服务器上的 SQL 服务器 - How to connect from ASP.NET Core application running in a container to SQL Server on Windows Server with Integrated Security 如何使用Visual Studio 2013连接到asp.net中的本地SQL Server数据库? - How to connect to a local SQL server database in asp.net using Visual Studio 2013? 如何使用SQL身份验证将作为“本地管理员”运行的应用程序连接到远程数据库,而不在服务器上使用“sysadmin”权限 - How do I connect application running as “local admin” to remote database using SQL Authentication, without “sysadmin” rights on the server 我正在尝试将 ASP.NET CORE API 版本 3.1 部署到本地 IIS 托管但无法连接到 Z970BCACB288 的服务器 - i am trying to deploy ASP.NET CORE API Version 3.1 to local IIS Hostiing but can not connect to SQL Server 从在 Docker Windows 容器中运行的 .Net 4.8 控制台应用程序连接到本地 SQL Server - Connect to local SQL Server from .Net 4.8 Console Application Running in Docker Windows container 无法从 Docker (Linux) 中运行的 ASP.NET Core 连接到 SQL Server 命名实例 - Can't connect to SQL Server named instance from ASP.NET Core running in Docker (Linux) ASP.NET MVC 无法连接到我的本地 SQL Server 数据库 - ASP.NET MVC cannot connect to my local SQL Server database 将图像插入ASP.NET中的本地SQL Server数据库 - Insert image to local SQL Server database in ASP.NET 如何在ASP.NET Core项目中指向我的完整SQL Server Express数据库? - How do I point to my full SQL Server Express database in ASP.NET Core project? 设置SQL帐户后,ASP.NET Core Web服务尝试使用本地计算机帐户连接到SQL Server - ASP.NET Core web service attempting to connect to SQL Server using local machine account when SQL account is set
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM