无法在部署的容器中可视化 Swagger UI
Unable to visualize Swagger UI in deployed container
我容器化了一个 .NET webapi 服务,包括 swagger 和 swagger UI。 dotnet 运行 运行 应用程序正确,但是当我将它容器化时,我变得无法可视化 UI
这是我的 dockerfile:
FROM mcr.microsoft.com/dotnet/aspnet:6.0-focal AS base
WORKDIR /app
EXPOSE 80
FROM mcr.microsoft.com/dotnet/sdk:6.0-focal AS build
WORKDIR /src
COPY ["DataHandlerAPI.csproj", "./"]
RUN dotnet restore "./DataHandlerAPI.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "DataHandlerAPI.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "DataHandlerAPI.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "DataHandlerAPI.dll"]
我应该更改什么才能使其 运行 与 dotnet run
一样正确
您需要将环境设置为 'Development',因为默认情况下 Swagger 仅在开发中可用。默认情况下 'Production' 环境中的容器 运行。
你可以这样设置
FROM mcr.microsoft.com/dotnet/aspnet:6.0-focal AS base
ENV ASPNETCORE_ENVIRONMENT=Development
WORKDIR /app
EXPOSE 80
FROM mcr.microsoft.com/dotnet/sdk:6.0-focal AS build
WORKDIR /src
COPY ["DataHandlerAPI.csproj", "./"]
RUN dotnet restore "./DataHandlerAPI.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "DataHandlerAPI.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "DataHandlerAPI.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "DataHandlerAPI.dll"]
我通过将这两行添加到我的 dockerfile 中解决了这个问题
ENV ASPNETCORE_URLS http://*:80
ENV ASPNETCORE_ENVIRONMENT=Development
我容器化了一个 .NET webapi 服务,包括 swagger 和 swagger UI。 dotnet 运行 运行 应用程序正确,但是当我将它容器化时,我变得无法可视化 UI
这是我的 dockerfile:
FROM mcr.microsoft.com/dotnet/aspnet:6.0-focal AS base
WORKDIR /app
EXPOSE 80
FROM mcr.microsoft.com/dotnet/sdk:6.0-focal AS build
WORKDIR /src
COPY ["DataHandlerAPI.csproj", "./"]
RUN dotnet restore "./DataHandlerAPI.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "DataHandlerAPI.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "DataHandlerAPI.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "DataHandlerAPI.dll"]
我应该更改什么才能使其 运行 与 dotnet run
您需要将环境设置为 'Development',因为默认情况下 Swagger 仅在开发中可用。默认情况下 'Production' 环境中的容器 运行。
你可以这样设置
FROM mcr.microsoft.com/dotnet/aspnet:6.0-focal AS base
ENV ASPNETCORE_ENVIRONMENT=Development
WORKDIR /app
EXPOSE 80
FROM mcr.microsoft.com/dotnet/sdk:6.0-focal AS build
WORKDIR /src
COPY ["DataHandlerAPI.csproj", "./"]
RUN dotnet restore "./DataHandlerAPI.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "DataHandlerAPI.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "DataHandlerAPI.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "DataHandlerAPI.dll"]
我通过将这两行添加到我的 dockerfile 中解决了这个问题
ENV ASPNETCORE_URLS http://*:80
ENV ASPNETCORE_ENVIRONMENT=Development