在 Docker 容器中运行 API 项目时无法本地浏览到 index.html

问题描述 投票:0回答:1

我正在尝试将 .NET API 设置为在 Docker 容器中运行并可通过浏览器访问。我在竞选本地发展时遇到了麻烦。存储库(特别是在开发分支上)位于here

我的 API 之前设置为与 UI 项目一起运行,该项目在我启动应用程序时默认运行,但我正在努力删除它(您可以在 API 的 csproj 文件中看到注释掉此处) 。我已将 wwwroot 添加到

rardk.web.API
项目,并且已卸载
rardk.web.UI
项目,但在将 API 项目作为启动项目运行时,我似乎没有获得测试 HTML 页面。

这是我的 Dockerfile:

#See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging.

FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
USER app
WORKDIR /app
EXPOSE 8080
EXPOSE 8081

FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
COPY ["rardk.web.UI/nuget.config", "rardk.web.UI/"]
COPY ["rardk.web.API/rardk.web.API.csproj", "rardk.web.API/"]
COPY ["rardk.web.BusinessLayer/rardk.web.BusinessLayer.csproj", "rardk.web.BusinessLayer/"]
COPY ["rardk.web.ServiceLayer/rardk.web.ServiceLayer.csproj", "rardk.web.ServiceLayer/"]
COPY ["rardk.web.Models/rardk.web.Models.csproj", "rardk.web.Models/"]
# COPY ["rardk.web.UI/rardk.web.UI.esproj", "rardk.web.UI/"]
RUN dotnet restore "./rardk.web.API/./rardk.web.API.csproj"
COPY . .
WORKDIR "/src/rardk.web.API"
RUN dotnet build "./rardk.web.API.csproj" -c $BUILD_CONFIGURATION -o /app/build

FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "./rardk.web.API.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "rardk.web.API.dll"]

和我的 launchSettings.json

{
  "profiles": {
    "http": {
      "commandName": "Project",
      "launchUrl": "swagger",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "dotnetRunMessages": true,
      "applicationUrl": "http://localhost:5227"
    },
    "https": {
      "commandName": "Project",
      "launchUrl": "swagger",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "dotnetRunMessages": true,
      "applicationUrl": "https://localhost:7042;http://localhost:5227"
    },
    "IIS Express": {
      "commandName": "IISExpress",
      "launchUrl": "swagger",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "Docker": {
      "commandName": "Docker",
      "launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}/swagger",
      "environmentVariables": {
        "ASPNETCORE_HTTPS_PORTS": "8081",
        "ASPNETCORE_HTTP_PORTS": "8080"
      },
      "publishAllPorts": true,
      "useSSL": true
    }
  },
  "$schema": "https://json.schemastore.org/launchsettings.json",
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:36857",
      "sslPort": 44319
    }
  }
}

这些是我的容器中的日志,包括我尝试浏览它时的 404:

2024-01-07 11:44:56 [16:44:56 INF] Now listening on: http://[::]:8080
2024-01-07 11:44:56 [16:44:56 INF] Now listening on: https://[::]:8081
2024-01-07 11:44:56 [16:44:56 INF] Application started. Press Ctrl+C to shut down.
2024-01-07 11:44:56 [16:44:56 INF] Hosting environment: Development
2024-01-07 11:44:56 [16:44:56 INF] Content root path: /app
2024-01-07 11:45:09 [16:45:09 INF] Request starting HTTP/1.1 GET http://localhost:32771/ - null null
2024-01-07 11:45:10 [16:45:10 INF] Request finished HTTP/1.1 GET http://localhost:32771/ - 307 0 null 130.1059ms
2024-01-07 11:45:10 [16:45:10 INF] Request starting HTTP/2 GET https://localhost:32770/ - null null
2024-01-07 11:45:10 [16:45:10 INF] Request finished HTTP/2 GET https://localhost:32770/index.html - 404 0 null 69.0943ms
2024-01-07 11:45:10 [16:45:10 INF] Request reached the end of the middleware pipeline without being handled by application code. Request path: GET https://localhost:32770/index.html, Response status code: 404
2024-01-07 11:45:50 [16:45:50 INF] Request starting HTTP/1.1 GET http://localhost:32771/ - null null
2024-01-07 11:45:50 [16:45:50 INF] Request finished HTTP/1.1 GET http://localhost:32771/ - 307 0 null 1.7569ms
2024-01-07 11:45:50 [16:45:50 INF] Request starting HTTP/2 GET https://localhost:32770/ - null null
2024-01-07 11:45:50 [16:45:50 INF] Request finished HTTP/2 GET https://localhost:32770/index.html - 404 0 null 84.2565ms
2024-01-07 11:45:50 [16:45:50 INF] Request reached the end of the middleware pipeline without being handled by application code. Request path: GET https://localhost:32770/index.html, Response status code: 404
c# docker asp.net-core .net-core
1个回答
0
投票

您正在开发模式下运行 docker 容器(来自日志):

托管环境:开发

但是您仅为非开发环境提供静态文件:

if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}
else
{
    app.UseDefaultFiles();
    app.UseStaticFiles(); // <- here
}

因此来自

index.html
wwwwroot
将不会在发展项目中提供服务。直接去掉else,无条件添加静态文件即可:

if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseDefaultFiles();
app.UseStaticFiles();
© www.soinside.com 2019 - 2024. All rights reserved.