docker - 在 docker 中托管后无法访问 .net core api URL

标签 docker asp.net-core windows-10

我有 VS 2017。Dotnet 核心 2.1 我的是 Windows 10 和 Docker for windows 安装。 Windows 版本为 10.0.16299.904 客户端和服务端的Docker版本都是community 18.09.0

我只是创建了一个新文件 -> 新建 -> 项目 -> .Net Core -> Asp.net Core Web 应用程序 -> Api 并启用了 Docker 复选框。

在不做任何更改的情况下,我构建了它,尝试在 IIS Express 和 Docker 中运行它,并且它在默认情况下将/api/values Controller 响应显示为值。

现在在 ps 提示符下我可以看到图像,因此创建了相应的容器。

在 docker ps -a 命令上,它列出了端口为 1598-> 80/tcp 的正在运行的容器

我使用 docker inspect 列出 json,显示的 ip 是 172.22.74.213。

我用了http://172.22.74.213:1598/api/values访问我的 api 但出现以下错误:

enter image description here

使用 docker inspect 表示容器正在运行。 我是否使用了错误的 URL 来访问我的 API?

最佳答案

是的,您使用了错误的 IP。只需点击 http://localhost:1598/api/values因为 Docker for Windows 不会(默认)为暴露的端口绑定(bind)到不同的 IP。

编辑

此外,请确保在运行容器时确实公开了端口。另请查看有关允许任何主机名从 this answer to a similar question 连接的 dotnet API 的详细信息:

check you are opening the port when launching your image too:

docker run -it -p 5000:5000 <imagename>

This will open port 5000 inside the instance to port 5000 on your local machine, and should then be accessible on 127.0.0.1:5000 or localhost:5000.

You should also ensure that you are accepting any host name within Main() in Program.cs with .UseUrls("http://*:5000/") on your WebHostBuilder.

在您的情况下,您将需要此端口绑定(bind):1598:80 .

关于docker - 在 docker 中托管后无法访问 .net core api URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54159795/

相关文章:

spring - 在 Windows 上使用 docker 开发 Spring Boot 应用程序,但在 Ubuntu VM 上运行 docker 容器(作为 Docker 主机)

c - 如果在带有 tmpfs 的容器中运行,为什么 meson 不能确定 time_t 的大小?

c# - ASP.NET Core 健康检查 : Returning pre-evaluated results

c# - SNS - JSON 消息正文解析失败

windows-10 - Apache Zeppelin 未加载到 Windows 10 的浏览器中

c++ - 如何最小化 USB 2 的延迟

mysql 安装程序在当前包中没有找到适合安装 windows 的包

docker - 在Bluemix中将卷与Redmine Docker容器一起使用

javascript - Deno 和 Docker 如何监听 SIGTERM 信号并关闭服务器

asp.net-core - ASP.Net 核心 : asp-action tag not working