Skip to content

Instantly share code, notes, and snippets.

@dariogriffo
Created June 12, 2023 07:40
Show Gist options
  • Save dariogriffo/183c643d9f3ea882a43a79d86bc6be92 to your computer and use it in GitHub Desktop.
Save dariogriffo/183c643d9f3ea882a43a79d86bc6be92 to your computer and use it in GitHub Desktop.
FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS base
WORKDIR /app
EXPOSE 80
FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
WORKDIR /src
COPY ["WebApi/WebApi.csproj", "WebApi/"]
RUN dotnet restore "WebApi/WebApi.csproj"
COPY . .
WORKDIR "/src/WebApi"
RUN dotnet build "WebApi.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "WebApi.csproj" -c Release -o /app/publish /p:UseAppHost=false
FROM base AS final
# Upgrade debian packages
RUN apt update && apt dist-upgrade -y
# Add non-root user
RUN adduser --disabled-password --home /app --gecos '' dotnetuser && chown -R dotnetuser /app
WORKDIR /app
# From here every command runs as non-root user
# including running your WebApi
USER dotnetuser
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "WebApi.dll"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment