I containerized a .NET webapi service including swagger and swagger UI. dotnet run runs the app correctly but when I containerized it I became unable to visualize the UI
Here is my 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"]
what should I change to make it run correctly as with dotnet run
CodePudding user response:
You need to set the environment to 'Development', since Swagger - by default - only is available in development. Containers run in 'Production' environment by default.
You can set it like this
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"]
CodePudding user response:
I resolved this issue by adding this line to my dockerfile
ENV ASPNETCORE_URLS http://*:80