asp.net - 为什么 aspnet 核心从 Docker 内部的端口 80 启动?

标签 asp.net docker .net-core kestrel-http-server

TL;DR: 为什么 aspnet 核心应用程序在 Docker 镜像内的端口 80 上运行,但在 Docker 镜像外的 5000 端口上运行。

详细说明

我浏览了此处找到的 aspnet core/docker 教程: https://learn.microsoft.com/en-us/dotnet/core/docker/building-net-docker-images

页面进行到一半,我按照规定使用以下内容启动应用程序:

dotnet run

除其他外,它会打印以下内容:

Now Listening on: http://localhost:5000

太好了。这就是我所期望的。本教程中的下一件事是从 Docker 镜像中启动完全相同的应用程序。

docker build -t aspnetapp .
docker run -it --rm -p 5000:80 --name aspnetcore_sample aspnetapp

这会导致

Now listening on: http://[::]:80

等等。笏? 为什么 aspnet 核心应用程序运行在 80 端口? 当我直接从机器运行它时,它运行在 5000 端口。没有配置文件更改。

我怀疑这与基础 docker 镜像有关,但我在 docker 方面还不够熟练,无法追踪到这一点。

最佳答案

microsoft/aspnetcore-build 容器构建在 之上 microsoft/aspnetcore 容器。 dockerhub page for that说:

A note on ports

This image sets the ASPNETCORE_URLS environment variable to http://+:80 which means that if you have not explicity set a URL in your application, via app.UseUrl in your Program.cs for example, then your application will be listening on port 80 inside the container.

所以这是容器主动将端口设置为 80。如果需要,您可以通过在 Dockerfile 中执行此操作来覆盖它:

ENV ASPNETCORE_URLS=http://+:5000

另外,值得注意的是,由于您使用的是 docker 命令,无论您是直接运行应用程序,您仍然可以通过 http://localhost:5000 访问应用程序或在容器中。

关于asp.net - 为什么 aspnet 核心从 Docker 内部的端口 80 启动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48669548/

相关文章:

c# - Azure Function 启动的配置未被调用

c# - Azure DevOps API 错误 : RuleValidationException: The field 'State' contains the value 'Active' that is not in the list of supported values C#

.net - msdeploy 忽略 .hgignore

docker - 无法安装 Docker Compose v2 : "Invalid Plugin, exec format error"

javascript - ASP.NET 内联服务器标签

docker - 尝试让我的 apache 容器响应 localhost :8080

docker - 为什么有些 docker 镜像在运行时需要命令?

linux - Linux 上不支持 EF Core 平台

c# - Entity Framework - 使用 lambda 表达式编写查询

c# - Repeater 中的 LinkBut​​ton 会导致回发,而按钮则不会