我有 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 但出现以下错误:
使用 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 on127.0.0.1:5000
orlocalhost:5000
.You should also ensure that you are accepting any host name within
Main()
inProgram.cs
with.UseUrls("http://*:5000/")
on yourWebHostBuilder
.
在您的情况下,您将需要此端口绑定(bind):1598:80
.
关于docker - 在 docker 中托管后无法访问 .net core api URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54159795/