Skip to content

Commit

Permalink
Separate OpenAPI document generation from build-env stage
Browse files Browse the repository at this point in the history
Signed-off-by: Yasumasa Suenaga <[email protected]>
  • Loading branch information
YaSuenag committed Apr 10, 2024
1 parent da7a254 commit 6799e5b
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions src/CarbonAware.WebApi/src/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,24 +1,33 @@
# For OpenAPI document
FROM --platform=$BUILDPLATFORM mcr.microsoft.com/dotnet/sdk:8.0 AS openapi-env
WORKDIR /app
ENV DOTNET_ROLL_FORWARD LatestMajor
COPY . ./
RUN dotnet build CarbonAware.WebApi/src/CarbonAware.WebApi.csproj -o build
WORKDIR /app/CarbonAware.WebApi/src
RUN dotnet tool restore && \
dotnet tool run swagger tofile --output /app/build/swagger.yaml --yaml /app/build/CarbonAware.WebApi.dll v1


# Builder
FROM --platform=$BUILDPLATFORM mcr.microsoft.com/dotnet/sdk:8.0 AS build-env
ARG TARGETARCH
WORKDIR /app

ENV DOTNET_ROLL_FORWARD LatestMajor
# Copy everything from source
COPY . ./
# Use implicit restore to build and publish
RUN dotnet publish CarbonAware.WebApi/src/CarbonAware.WebApi.csproj -a $TARGETARCH -o publish
# Generate OpenAPI spec
WORKDIR /app/CarbonAware.WebApi/src
RUN dotnet tool restore && \
mkdir -p /app/publish/wwwroot/api/v1 && \
dotnet tool run swagger tofile --output /app/publish/wwwroot/api/v1/swagger.yaml --yaml /app/publish/CarbonAware.WebApi.dll v1


# Build runtime image
FROM mcr.microsoft.com/dotnet/aspnet:8.0
# Install curl for health check
RUN apt-get update && \
apt-get install -y --no-install-recommends curl
# Copy artifacts from build-env
# Copy artifacts
WORKDIR /app
COPY --from=build-env /app/publish .
RUN mkdir -p /app/wwwroot/api/v1
COPY --from=openapi-env /app/build/swagger.yaml /app/wwwroot/api/v1/
ENTRYPOINT ["dotnet", "CarbonAware.WebApi.dll"]

0 comments on commit 6799e5b

Please sign in to comment.