为什么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核心/ docker教程:
https://docs.microsoft .com / zh-CN / 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
在其他事物中,此打印如下:
Among other things, this prints this:
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
容器构建在
容器。 dockerhub页面表示:
microsoft / aspnetcore
The microsoft/aspnetcore-build
container builds on top of the
microsoft/aspnetcore
container. The dockerhub page for that says:
端口说明
A note on ports
此图像将ASPNETCORE_URLS环境变量设置为 http:// +:80 ,这意味着如果您没有通过在Program.cs中通过app.UseUrl在应用程序中显式设置URL,则您的应用程序将侦听容器内部的端口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
另外,值得注意的是,由于您使用的是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屋!