Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
859 views
in Technique[技术] by (71.8m points)

asp.net - Why does aspnet core start on port 80 from within Docker?

TL;DR: Why does an aspnet core app run on port 80 from within a Docker image, but 5000 outside a docker image.

Elaborate

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

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

This results in

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

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.

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The microsoft/aspnetcore-build container builds on top of the microsoft/aspnetcore container. The dockerhub page for that says:

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.

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...