【问题标题】:Cannot hit Dockerized .net core application using localhost and port无法使用 localhost 和端口访问 Dockerized .net 核心应用程序
【发布时间】:2020-12-26 21:09:35
【问题描述】:

我有 .net core 3.1 dockerized 及更高版本 (docker-compose up --build) 来自 docker 容器,但我仍然无法使用 http://localhost:8080/http://127.0.0.1:8080/ 从应用程序访问任何招摇网址

launchSettings.json

{
  "$schema": "http://json.schemastore.org/launchsettings.json",
    "MyApp.API": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "swagger",
      "applicationUrl": "https://0.0.0.0:5001;http://0.0.0.0:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
}

Dockerfile

FROM mcr.microsoft.com/dotnet/core/sdk:3.1
WORKDIR /home/app 
Copy ... projedct files ommited on purpose for clarity ...
RUN dotnet restore 
COPY . . 
RUN dotnet publish MyApp.API/MyApp.API.csproj -o /publish/ 
WORKDIR /publish 
ENV ASPNETCORE_URLS=https://+:5001;http://+:5000
ENTRYPOINT ["dotnet", "MyApp.API.dll"]

docker-compose.yml

version: '3.0' 
services:  
  api:
    ports: 
      - "8080:5000"
      - "8081:5001"       
    build:
      context: .      
      dockerfile: MyApp.API/Dockerfile
    image: myapp/api:runtime   

【问题讨论】:

  • 你没有暴露端口 5000 和 5001
  • 尝试EXPOSE容器中的端口

标签: docker .net-core


【解决方案1】:

您为 http(s) 请求使用端口 5000 和 5001 而不是 80 和 443 是否有原因?如果您使用的是 Visual Studio,它将为您生成一个 Dockerfile,默认情况下会公开端口 80 和 443。如果你真的想使用 5000 和 5001,你可以在你的 Dockerfile 中公开它们。

FROM mcr.microsoft.com/dotnet/core/sdk:3.1
WORKDIR /home/app 
EXPOSE 5000
EXPOSE 5001
Copy ... projedct files ommited on purpose for clarity ...
RUN dotnet restore 
COPY . . 
RUN dotnet publish MyApp.API/MyApp.API.csproj -o /publish/ 
WORKDIR /publish 
ENV ASPNETCORE_URLS=https://+:5001;http://+:5000
ENTRYPOINT ["dotnet", "MyApp.API.dll"]

【讨论】:

  • 或在您的撰写文件中
猜你喜欢
  • 1970-01-01
  • 2022-08-02
  • 1970-01-01
  • 2021-12-03
  • 2023-02-01
  • 1970-01-01
  • 2022-01-08
  • 2018-08-16
  • 1970-01-01
相关资源
最近更新 更多