From 0a0877dc4d2a369336f137e908faad0918e05884 Mon Sep 17 00:00:00 2001 From: khwezi Date: Tue, 3 Mar 2026 17:34:28 +0200 Subject: [PATCH] Refactored dockerfile --- Dockerfile | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/Dockerfile b/Dockerfile index 67d477b..057693c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,21 +2,34 @@ FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build WORKDIR /src -# Copy the solution and all project files to restore dependencies first (better caching) +# 1. Copy the .sln and .csproj files first to restore dependencies +# This makes builds faster by caching the 'restore' layer +COPY ["SampleApi/SampleApi.csproj", "SampleApi/"] +RUN dotnet restore "SampleApi/SampleApi.csproj" + +# 2. Copy the rest of the code COPY . . -# Run publish. -# We use --no-self-contained to keep it small since the runtime image has the shared framework. -RUN dotnet publish "SampleApi/SampleApi.csproj" -c Release -o /app/publish /p:UseAppHost=false +# 3. Publish to a FLAT directory +# We use -o /app/publish and ensure no extra subfolders are created +RUN dotnet publish "SampleApi/SampleApi.csproj" \ + -c Release \ + -o /app/publish \ + --no-restore \ + /p:UseAppHost=false # STAGE 2: Runtime FROM mcr.microsoft.com/dotnet/aspnet:8.0 WORKDIR /app -# Copy the contents of the publish folder directly into /app +# Copy the contents directly COPY --from=build /app/publish . -# Pro-tip: Ensure the port matches your docker-compose +# DEBUG: This will print the contents of /app during the build. +# If SampleApi.dll isn't in this list, the build will stop here. +RUN ls -la /app + +# Set the environment to listen on the port you mapped in Compose ENV ASPNETCORE_HTTP_PORTS=8081 ENTRYPOINT ["dotnet", "SampleApi.dll"] \ No newline at end of file