🟣 High-Performance .NET Multi-Stage
Production Dockerfile for ASP.NET Core
Multi-stage build compiling C# applications with the full .NET SDK and running with the optimized ASP.NET Core runtime image.
Dockerfile
.NET 8.0 SDK & Runtime
# 1. SDK build stage
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
WORKDIR /src
# Copy project file and restore NuGet packages
COPY *.csproj ./
RUN dotnet restore
# Copy source and publish release binaries
COPY . ./
RUN dotnet publish -c Release -o /app/publish --no-restore
# 2. Optimized runtime stage
FROM mcr.microsoft.com/dotnet/aspnet:8.0-alpine AS runtime
WORKDIR /app
ENV ASPNETCORE_URLS=http://+:8080
ENV DOTNET_EnableDiagnostics=0
# Use non-root container user built into modern .NET images
USER $APP_UID
COPY --from=build /app/publish .
EXPOSE 8080
ENTRYPOINT ["dotnet", "MyApi.dll"]
Key Highlights
- SDK vs Runtime: The massive 800MB+ SDK image is discarded; the final Alpine runtime footprint is only ~100MB.
- USER $APP_UID: Uses official .NET 8 non-root UID 1654 for strict container security standards.
- Layer Optimization:
dotnet restoreruns prior to copying application logic to leverage Docker caching.