|
1 | | -# - Stage 1 -------------------------------------------------------------------- |
| 1 | +# ------------------------------------------------------------------------------ |
| 2 | +# Stage 1: Builder |
| 3 | +# This stage builds the application and its dependencies. |
| 4 | +# ------------------------------------------------------------------------------ |
| 5 | +FROM mcr.microsoft.com/dotnet/sdk:8.0 AS builder |
2 | 6 |
|
3 | | - FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build |
4 | | - WORKDIR /src |
| 7 | +WORKDIR /src |
5 | 8 |
|
6 | | - # Copy and restore dependencies |
7 | | - COPY src/Dotnet.Samples.AspNetCore.WebApi/*.csproj ./Dotnet.Samples.AspNetCore.WebApi/ |
8 | | - RUN dotnet restore ./Dotnet.Samples.AspNetCore.WebApi |
| 9 | +# Restore dependencies |
| 10 | +COPY src/Dotnet.Samples.AspNetCore.WebApi/*.csproj ./Dotnet.Samples.AspNetCore.WebApi/ |
| 11 | +RUN dotnet restore ./Dotnet.Samples.AspNetCore.WebApi |
9 | 12 |
|
10 | | - # Copy source and publish |
11 | | - COPY src/Dotnet.Samples.AspNetCore.WebApi ./Dotnet.Samples.AspNetCore.WebApi |
12 | | - WORKDIR /src/Dotnet.Samples.AspNetCore.WebApi |
13 | | - RUN dotnet publish -c Release -o /app/publish |
| 13 | +# Copy source code and pre-seeded SQLite database |
| 14 | +COPY src/Dotnet.Samples.AspNetCore.WebApi ./Dotnet.Samples.AspNetCore.WebApi |
14 | 15 |
|
15 | | -# - Stage 2 -------------------------------------------------------------------- |
| 16 | +WORKDIR /src/Dotnet.Samples.AspNetCore.WebApi |
16 | 17 |
|
17 | | - FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS runtime |
18 | | - WORKDIR /app |
| 18 | +# Build solution and publish release |
| 19 | +RUN dotnet publish -c Release -o /app/publish |
19 | 20 |
|
20 | | - # Copy published output |
21 | | - # Note: This includes the SQLite database because it's marked as <Content> with |
22 | | - # <CopyToOutputDirectory> in the .csproj file. No need to copy it manually. |
23 | | - COPY --from=build /app/publish . |
| 21 | +# ------------------------------------------------------------------------------ |
| 22 | +# Stage 2: Runtime |
| 23 | +# This stage creates the final, minimal image to run the application. |
| 24 | +# ------------------------------------------------------------------------------ |
| 25 | +FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS runtime |
24 | 26 |
|
25 | | - # Add non-root user (aspnetcore) for security hardening |
26 | | - RUN adduser --disabled-password --gecos '' aspnetcore \ |
27 | | - && chown -R aspnetcore:aspnetcore /app |
28 | | - USER aspnetcore |
| 27 | +WORKDIR /app |
29 | 28 |
|
30 | | - # Set environment variables |
31 | | - ENV ASPNETCORE_URLS=http://+:9000 |
32 | | - ENV ASPNETCORE_ENVIRONMENT=Production |
| 29 | +# Install curl for health check |
| 30 | +RUN apt-get update && apt-get install -y --no-install-recommends curl && \ |
| 31 | + rm -rf /var/lib/apt/lists/* |
33 | 32 |
|
34 | | - # Default entrypoint |
35 | | - ENTRYPOINT ["dotnet", "Dotnet.Samples.AspNetCore.WebApi.dll"] |
| 33 | +# Metadata labels for the image. These are useful for registries and inspection. |
| 34 | +LABEL org.opencontainers.image.title="🧪 Web API made with .NET 8 (LTS) and ASP.NET Core" |
| 35 | +LABEL org.opencontainers.image.description="Proof of Concept for a Web API made with .NET 8 (LTS) and ASP.NET Core" |
| 36 | +LABEL org.opencontainers.image.licenses="MIT" |
| 37 | +LABEL org.opencontainers.image.source="https://github.com/nanotaboada/Dotnet.Samples.AspNetCore.WebApi" |
| 38 | + |
| 39 | +# Set environment variables |
| 40 | +ENV ASPNETCORE_URLS=http://+:9000 |
| 41 | +ENV ASPNETCORE_ENVIRONMENT=Production |
| 42 | + |
| 43 | +# Copy published app from builder |
| 44 | +COPY --from=builder /app/publish . |
| 45 | + |
| 46 | +# Copy metadata docs for container registries (e.g.: GitHub Container Registry) |
| 47 | +COPY --chmod=444 README.md ./ |
| 48 | +COPY --chmod=555 assets ./assets |
| 49 | + |
| 50 | +# https://rules.sonarsource.com/docker/RSPEC-6504/ |
| 51 | + |
| 52 | +# Copy entrypoint and healthcheck scripts |
| 53 | +COPY --chmod=555 scripts/entrypoint.sh ./entrypoint.sh |
| 54 | +COPY --chmod=555 scripts/healthcheck.sh ./healthcheck.sh |
| 55 | + |
| 56 | + |
| 57 | +# Copy pre-seeded SQLite database as init bundle |
| 58 | +COPY --from=builder /src/Dotnet.Samples.AspNetCore.WebApi/storage/players-sqlite3.db ./docker-compose/players-sqlite3.db |
| 59 | + |
| 60 | +# Create non-root user and make volume mount point writable |
| 61 | +RUN adduser --disabled-password --gecos '' aspnetcore && \ |
| 62 | + mkdir -p /storage && \ |
| 63 | + chown -R aspnetcore:aspnetcore /storage |
| 64 | + |
| 65 | +USER aspnetcore |
| 66 | + |
| 67 | +HEALTHCHECK --interval=30s --timeout=5s --start-period=5s --retries=3 \ |
| 68 | + CMD ["./healthcheck.sh"] |
| 69 | + |
| 70 | +EXPOSE 9000 |
| 71 | + |
| 72 | +ENTRYPOINT ["./entrypoint.sh"] |
| 73 | +CMD ["dotnet", "Dotnet.Samples.AspNetCore.WebApi.dll"] |
0 commit comments