diff --git a/Webshop.Api/Dockerfile b/Webshop.Api/Dockerfile index 8911894..774bce7 100644 --- a/Webshop.Api/Dockerfile +++ b/Webshop.Api/Dockerfile @@ -1,25 +1,37 @@ -#See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging. - -FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base -USER app -WORKDIR /app -EXPOSE 8080 -EXPOSE 8081 - +# Phase 1: Die Build-Phase +# Wir verwenden das offizielle .NET 8 SDK-Image, das alle Werkzeuge zum Bauen enthält. FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build -ARG BUILD_CONFIGURATION=Release WORKDIR /src + +# Kopiere zuerst die Projekt- und Solution-Dateien. +# Docker ist schlau: Wenn sich diese Dateien nicht ändern, verwendet es den Cache und der 'restore'-Schritt wird übersprungen. +COPY ["ShopSolution.sln", "."] COPY ["Webshop.Api/Webshop.Api.csproj", "Webshop.Api/"] -RUN dotnet restore "./Webshop.Api/Webshop.Api.csproj" +COPY ["Webshop.Application/Webshop.Application.csproj", "Webshop.Application/"] +COPY ["Webshop.Domain/Webshop.Domain.csproj", "Webshop.Domain/"] +COPY ["Webshop.Infrastructure/Webshop.Infrastructure.csproj", "Webshop.Infrastructure/"] + +# Lade alle NuGet-Abhängigkeiten für die gesamte Solution herunter. +RUN dotnet restore "ShopSolution.sln" + +# Kopiere den gesamten restlichen Quellcode. COPY . . + +# Wechsle in das Verzeichnis des API-Projekts. WORKDIR "/src/Webshop.Api" -RUN dotnet build "./Webshop.Api.csproj" -c $BUILD_CONFIGURATION -o /app/build +# Baue und publiziere die Anwendung in einer Release-Konfiguration. +# Das Ergebnis wird in den Ordner /app/publish verschoben. +RUN dotnet publish "Webshop.Api.csproj" -c Release -o /app/publish --no-restore -FROM build AS publish -ARG BUILD_CONFIGURATION=Release -RUN dotnet publish "./Webshop.Api.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false +# --- -FROM base AS final +# Phase 2: Die finale Runtime-Phase +# Wir verwenden das deutlich kleinere ASP.NET Core Runtime-Image, da wir den Code nicht mehr kompilieren müssen. +FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS final WORKDIR /app -COPY --from=publish /app/publish . + +# Kopiere nur die publizierte Anwendung aus der Build-Phase. +COPY --from=build /app/publish . + +# Definiere den Befehl, der ausgeführt wird, wenn der Container startet. ENTRYPOINT ["dotnet", "Webshop.Api.dll"] \ No newline at end of file