简体   繁体   English

.NET 项目无法从主机操作系统通过 localhost 连接到 WSL2 中的 Docker 容器

[英].NET project not able to connect to Docker container in WSL2 via localhost from the host OS

I am not using Docker Desktop, I only installed the CLI on Ubuntu, on WSL2.我没有使用 Docker 桌面,我只在 WSL2 上的 Ubuntu 上安装了 CLI。 I also downloaded RabbitMQ image and successfully connected to localhost:15672 from my browser on Windows host.我还下载了 RabbitMQ 图像并从 Windows 主机上的浏览器成功连接到 localhost:15672。

But, when it came to the .NET project, it wasn't possible.但是,当涉及到 .NET 项目时,这是不可能的。 This is the connection string:这是连接字符串:

"host=localhost:5672;publisherConfirms=true;timeout=25;username=nerdstore;password=!abcd1234!" “主机=本地主机:5672;publisherConfirms=true;超时=25;用户名=nerdstore;密码=!abcd1234!”

One solution was to use eth0's inet IP address from within Ubuntu, but I have many projects using this connection, it is a pain to modify it every day, at least.一种解决方案是从 Ubuntu 中使用 eth0 的 inet IP 地址,但我有很多项目使用此连接,至少每天都修改它很痛苦。

"host=123.45.67.890:5672;publisherConfirms=true;timeout=25;username=nerdstore;password=!abcd1234!" “主机=123.45.67.890:5672;publisherConfirms=true;超时=25;用户名=nerdstore;密码=!abcd1234!”

Why is that I am able to connect to the container via browser but not form the .NET project and how could I manage to do it?为什么我能够通过浏览器连接到容器但不能形成 .NET 项目,我该如何做到这一点?

Add your Ubuntu IP 123.45.67.890 into hosts file with your custom hostname wsl2.ubuntu by below command.使用您的自定义主机名wsl2.ubuntu将您的 Ubuntu IP 123.45.67.890添加到 hosts 文件中

Make sure you run PowerShell as administrator.确保以管理员身份运行 PowerShell。

Add-Content -Path $env:windir\System32\drivers\etc\hosts -Value "`n123.45.67.890`twsl2.ubuntu" -Force

Then you connection would be like this然后你的连接会是这样的

"host=wsl2.ubuntu:5672;publisherConfirms=true;timeout=25;username=nerdstore;password=!abcd1234!" “主机=wsl2.ubuntu:5672;publisherConfirms=true;timeout=25;username=nerdstore;password=!abcd1234!”

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法从 Windows 主机连接到 WSL2 上的本地服务器 - Unable to connect to local server on WSL2 from Windows host 通过 https://localhost:8000/ 访问时,Localhost 拒绝在 WSL2 上连接,但在使用内部 WSL IP 地址时有效 - Localhost refused to connect on WSL2 when accessed via https://localhost:8000/ but works when using internal WSL IP adress Windows 主机 Docker + WSL2 - 如何将 Windows 目录挂载到 ZEDC9F0A5A5D57797BF68E373674 容器 - Windows Host Docker + WSL2 - How to mount Windows directory to a Linux container 从 WSL2 内的 Docker 容器启动 Windows 10 中的 WebApp(Windows 没有 Docker) - Launch WebApp in Windows 10 from Docker container inside WSL2 (without Docker for Windows) 使用实际 IP 从 WSL2 连接到主机 - Connectivity from WSL2 to host by using actual IP Windows上的Docker:如何使用容器IP从主机连接到容器? - Docker on Windows: how to connect to container from host using container IP? 如何从 windows Z05B6053C415A2134EAD6 容器中的 .NET 应用程序连接到在主机上运行的 SQL 服务器 - How do you connect to SQL Server running on the host from a .NET app in a windows docker container 无法从 Windows IntelliJ 在 WSL2 中编译 Maven 项目 - Can't compile Maven project in WSL2 from Windows IntelliJ 如何从主机外部(同一网络)连接到 docker 容器 [Windows] - How to connect to a docker container from outside the host (same network) [Windows] 无法从主机连接到 Docker 容器中的 static 网站 - Can't connect to static website in Docker container from host
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM