简体   繁体   English

容器网络如何处理与.Net和Docker的端口映射?

[英]How does the container networking handle the port mapping with .Net and Docker?

When creating a new.Net 5 Web Api project you can find this in the generated launchSettings.json file创建新的.Net 5 Web Api 项目时,您可以在生成的launchSettings.json文件中找到它

"applicationUrl": "https://localhost:5001;http://localhost:5000",

So when running the project you can call the Api endpoint via因此,在运行项目时,您可以通过以下方式调用 Api 端点

GET https://localhost:5001/weatherforecast获取 https://localhost:5001/weatherforecast

When adding Docker support for the project you might create a Dockerfile like为项目添加 Docker 支持时,您可能会创建Dockerfile 之类的

FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /app

COPY *.csproj ./
RUN dotnet restore

COPY . ./
RUN dotnet publish -c Release -o out

FROM mcr.microsoft.com/dotnet/aspnet
WORKDIR /app
EXPOSE 80
COPY --from=build /app/out .
ENTRYPOINT [ "dotnet", "Api.dll" ]

Most sample files expose port 80 in that file.大多数示例文件在该文件中公开端口 80。 When running the Api in a Docker container via通过在 Docker 容器中运行 Api 时

docker run -p 8080:80 my-image

I have to change the call to我必须将呼叫更改为

GET http://localhost:8080/weatherforecast获取 http://localhost:8080/weatherforecast

So I call port 8080 which maps to port 80 internally but how does port 80 map to port 5000 to forward the request to the Api?所以我调用端口 8080,它在内部映射到端口 80,但是端口 80 map 到端口 5000 如何将请求转发到 Api? It works "somehow".它“以某种方式”工作。 Would someone mind explaining this container networking?有人介意解释这个容器网络吗?

As docs say launchSettings.json file is used only for local development:正如文档所说launchSettings.json文件仅用于本地开发:

The launchSettings.json file: launchSettings.json 文件:

  • Is only used on the local development machine.仅在本地开发机器上使用。
  • Is not deployed.未部署。
  • contains profile settings.包含配置文件设置。

As for docker image - it has environment variable ASPNETCORE_URLS set to http://+:80 so app listens to 80 port and there is no mapping happening between 80 and 5000至于 docker 图像 - 它的环境变量ASPNETCORE_URLS设置为http://+:80所以应用程序监听80端口并且在805000之间没有发生映射

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM