Docker (1 / 5): Can you compose a Dockerfile for a .NET Core 3.1 web application, named "WebApplication1", which is capable of serving both secure and non-secure web traffic? The source code for the application resides in "/src/WebApplication1". How would you structure the Dockerfile stages to ensure the final image is lean and the build process adheres to best practices? Lastly, ensure that the application starts by executing a DLL file.
FROM mcr.microsoft.com/dotnet/core # Fill in details
Answer:
Note: Because we are exposing our app, FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 is in the first step, not the last
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build
WORKDIR /src
COPY ["WebApplication1/WebApplication1.csproj", "WebApplication1/"]
RUN dotnet restore "WebApplication1/WebApplication1.csproj"
COPY . .
WORKDIR "/src/WebApplication1"
RUN dotnet build "WebApplication1.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "WebApplication1.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "WebApplication1.dll"]