Refactored dockerfile
This commit is contained in:
+14
-1
@@ -1,20 +1,33 @@
|
|||||||
|
# Stage 1: Build environment
|
||||||
FROM mcr.microsoft.com/dotnet/sdk:10.0-bookworm-slim AS build
|
FROM mcr.microsoft.com/dotnet/sdk:10.0-bookworm-slim AS build
|
||||||
|
# 1. Explicitly run as root for the build stage
|
||||||
|
USER root
|
||||||
WORKDIR /src
|
WORKDIR /src
|
||||||
|
|
||||||
|
# Copy the project file and restore dependencies
|
||||||
COPY ["LiteCharmsMessaging/LiteCharmsMessaging.csproj", "LiteCharmsMessaging/"]
|
COPY ["LiteCharmsMessaging/LiteCharmsMessaging.csproj", "LiteCharmsMessaging/"]
|
||||||
RUN dotnet restore "LiteCharmsMessaging/LiteCharmsMessaging.csproj"
|
RUN dotnet restore "LiteCharmsMessaging/LiteCharmsMessaging.csproj"
|
||||||
|
|
||||||
|
# Copy the rest of the source code
|
||||||
COPY . .
|
COPY . .
|
||||||
|
|
||||||
|
# Move to the project directory and build the release
|
||||||
WORKDIR "/src/LiteCharmsMessaging"
|
WORKDIR "/src/LiteCharmsMessaging"
|
||||||
RUN dotnet build "LiteCharmsMessaging.csproj" -c Release -o /app/build
|
RUN dotnet build "LiteCharmsMessaging.csproj" -c Release -o /app/build
|
||||||
RUN dotnet publish "LiteCharmsMessaging.csproj" -c Release -o /app/publish /p:UseAppHost=false
|
RUN dotnet publish "LiteCharmsMessaging.csproj" -c Release -o /app/publish /p:UseAppHost=false
|
||||||
|
|
||||||
|
# Stage 2: Runtime environment
|
||||||
FROM mcr.microsoft.com/dotnet/aspnet:10.0-bookworm-slim AS final
|
FROM mcr.microsoft.com/dotnet/aspnet:10.0-bookworm-slim AS final
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
COPY --from=build /app/publish .
|
# 2. Switch to the built-in non-root user ('app') before copying files
|
||||||
|
USER $APP_UID
|
||||||
|
|
||||||
|
# 3. Copy the published output and transfer ownership to the non-root user
|
||||||
|
COPY --chown=$APP_UID:$APP_UID --from=build /app/publish .
|
||||||
|
|
||||||
|
# .NET 8+ defaults to port 8080 for web apps running as non-root
|
||||||
EXPOSE 8080
|
EXPOSE 8080
|
||||||
|
|
||||||
|
# Start the application
|
||||||
ENTRYPOINT ["dotnet", "LiteCharmsMessaging.dll"]
|
ENTRYPOINT ["dotnet", "LiteCharmsMessaging.dll"]
|
||||||
Reference in New Issue
Block a user