无法从主机连接到容器内的暴露端口



我有一个简单的应用程序,它监听端口5001上的http请求并返回"hello"当HTTP请求到达该端口时。然后我为应用程序构建图像并运行端口映射docker run -dp 5001:5001 --name web-server web-server的容器,但是当我转到浏览器并点击localhost:5001时,它就不起作用了。所以我的ssh容器和curl localhost:5001,这个作品。你能告诉我什么可能是错误的或错误的配置吗?

这是我的Dockerfile文件

FROM mcr.microsoft.com/dotnet/aspnet:5.0
WORKDIR /app
COPY ./bin/Release/net5.0 .
EXPOSE 5001
ENTRYPOINT ["dotnet", "WebServerFromScratch.dll"]

docker container inspect

中的NetworkSettings
"Ports": {
"5001/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "5001"
},
{
"HostIp": "::",
"HostPort": "5001"
}]
},
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "f227d735df3f23edc2f2aaf1a800bdeb70cf60caf50e08dc6b594b94cb38a1fb",
"EndpointID": "824ee71d4ed0a7e08080aeb728cf1f6821c5cdd7e2afbd6f1ca0327e68e47d44",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:02",
"DriverOpts": null
}
}

在微软的aspnetdocker映像中,有一个将ASPNETCORE_URLS环境变量定义为http://+:80的值,该值使dockerized应用程序默认侦听端口80。

如果您查看来自容器的日志,您应该能够看到一条消息,它正在侦听端口80,如下所示

info: Microsoft.Hosting.Lifetime[0]
Now listening on: http://[::]:80

所以当你运行它的时候,你应该把外部端口映射到内部的80端口,像这样

docker run -dp 5001:80 --name web-server web-server

那么您应该能够通过curl http://localhost:5001/从主机访问它。