为什么 aspnet 核心从 Docker 内部的端口 80 上启动? [英] Why does aspnet core start on port 80 from within Docker?
问题描述
TL;DR: 为什么 aspnet 核心应用程序在 Docker 映像内的端口 80 上运行,而在 Docker 映像外运行 5000.
TL;DR: Why does an aspnet core app run on port 80 from within a Docker image, but 5000 outside a docker image.
精心制作
我浏览了在此处找到的 aspnet core/docker 教程:https://docs.microsoft.com/en-us/dotnet/core/docker/building-net-docker-images
I went through the aspnet core / docker tutorial found here: https://docs.microsoft.com/en-us/dotnet/core/docker/building-net-docker-images
页面进行到一半时,我按照规定使用以下内容启动应用程序:
Half way through the page, I start the application with the following as prescribed:
dotnet run
除其他外,这会打印:
Now Listening on: http://localhost:5000
太好了.这正是我所期望的.本教程的下一步是从 Docker 映像中启动完全相同的应用程序.
Great. That is what I expected. The next thing in the tutorial is to start the exact same application from within a Docker image.
docker build -t aspnetapp .
docker run -it --rm -p 5000:80 --name aspnetcore_sample aspnetapp
结果是
Now listening on: http://[::]:80
等等.嬷嬷?为什么aspnet核心应用运行在80端口?当我直接从机器上运行它时,它运行在5000端口.没有配置文件更改.
Wait. Wat? Why is the aspnet core app running on port 80? It was running on port 5000 when I ran it directly from the machine. There were no configuration file changes.
我怀疑这与基本的 docker 镜像有关,但我对 docker 还不够熟练,无法追踪到这一点.
I suspect that it has something to do with the base docker images, but am not yet skilled enough in docker to track this down.
推荐答案
microsoft/aspnetcore-build
容器构建在 之上microsoft/aspnetcore
容器.dockerhub 页面 说:
The microsoft/aspnetcore-build
container builds on top of the
microsoft/aspnetcore
container. The dockerhub page for that says:
关于端口的说明
此图片将 ASPNETCORE_URLS 环境变量设置为 http://+:80 这意味着如果您没有明确设置应用程序中的 URL,例如通过 Program.cs 中的 app.UseUrl,然后您的应用程序将侦听容器内的端口 80.
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
中执行此操作来覆盖它:
So this is the container actively setting the port to 80. You can override it, if you want, by doing this in your Dockerfile
:
ENV ASPNETCORE_URLS=http://+:5000
ENV ASPNETCORE_URLS=http://+:5000
另外,值得注意的是,由于您使用的docker命令,无论您是否直接运行应用程序,您仍然可以通过http://localhost:5000
访问应用程序或在容器中.
Also, it is worth noting that because of the docker command you are using, you will still be able to access the application at http://localhost:5000
whether you are running the application directly or in a container.
这篇关于为什么 aspnet 核心从 Docker 内部的端口 80 上启动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!