🔧 Adds Dockerfile for generic multi-stage build and deployment setup
This commit is contained in:
119
docker/Dockerfile
Normal file
119
docker/Dockerfile
Normal file
@@ -0,0 +1,119 @@
|
||||
# syntax=docker/dockerfile:1.20
|
||||
ARG RUNTIME_BASE=app
|
||||
ARG CONFIGURATION=Release
|
||||
ARG DOTNET_VERSION=10.0
|
||||
ARG RUNTIME_ID=linux-musl-x64
|
||||
|
||||
FROM mcr.microsoft.com/dotnet/aspnet:${DOTNET_VERSION}-alpine-composite-extra AS web
|
||||
WORKDIR /app
|
||||
|
||||
FROM mcr.microsoft.com/dotnet/runtime:${DOTNET_VERSION}-alpine-extra AS app
|
||||
WORKDIR /app
|
||||
|
||||
FROM mcr.microsoft.com/dotnet/runtime-deps:${DOTNET_VERSION}-alpine-extra AS self-contained
|
||||
WORKDIR /app
|
||||
|
||||
FROM mcr.microsoft.com/dotnet/sdk:${DOTNET_VERSION} AS sdk
|
||||
ENV DOTNET_NOLOGO=1
|
||||
ENV DOTNET_CLI_TELEMETRY_OPTOUT=1
|
||||
ARG RUNTIME_ID
|
||||
WORKDIR /source
|
||||
|
||||
FROM sdk AS restore
|
||||
|
||||
COPY --link Directory.* stylecop.json xunit.runner.json *.sln *.slnx *.config ./
|
||||
COPY --link --parents ./**.csproj ./**/packages.lock.json ./
|
||||
|
||||
RUN --mount=type=cache,target=/root/.nuget/packages \
|
||||
dotnet restore && \
|
||||
dotnet restore --runtime ${RUNTIME_ID}
|
||||
|
||||
FROM restore AS build
|
||||
ARG CONFIGURATION
|
||||
|
||||
COPY --link src/ src/
|
||||
COPY --link tests/ tests/
|
||||
COPY --link samples/ samples/
|
||||
COPY --link .git/ ./.git/
|
||||
|
||||
RUN --mount=type=cache,target=/root/.nuget/packages \
|
||||
dotnet build --no-restore --configuration ${CONFIGURATION}
|
||||
|
||||
FROM build AS test
|
||||
ARG CONFIGURATION
|
||||
|
||||
RUN dotnet test --no-build --configuration ${CONFIGURATION}
|
||||
|
||||
FROM test AS publish
|
||||
ARG CONFIGURATION
|
||||
ARG PUBLISHED_PROJECT
|
||||
ARG RUNTIME_BASE
|
||||
ENV PROJECT=src/${PUBLISHED_PROJECT}/${PUBLISHED_PROJECT}.csproj
|
||||
|
||||
RUN --mount=type=cache,target=/root/.nuget/packages \
|
||||
if [ "${RUNTIME_BASE}" = "self-contained" ]; then \
|
||||
dotnet publish --configuration ${CONFIGURATION} \
|
||||
--self-contained --runtime ${RUNTIME_ID} \
|
||||
-o /app/publish ${PROJECT}; \
|
||||
else \
|
||||
dotnet publish --configuration ${CONFIGURATION} \
|
||||
--no-self-contained --runtime ${RUNTIME_ID} \
|
||||
-o /app/publish ${PROJECT}; \
|
||||
fi
|
||||
|
||||
FROM build AS pack
|
||||
|
||||
RUN --mount=type=cache,target=/root/.nuget/packages \
|
||||
dotnet pack --configuration Release --no-build --output /app/publish
|
||||
|
||||
FROM sdk AS nuget-push
|
||||
WORKDIR /packages
|
||||
COPY --link --from=pack /app/publish/*.nupkg .
|
||||
ENTRYPOINT ["sh", "-c", "dotnet nuget push /packages/*.nupkg --source ${NUGET_SOURCE} --api-key ${NUGET_API_KEY} --skip-duplicate"]
|
||||
CMD []
|
||||
|
||||
FROM build AS migration-bundle
|
||||
RUN --mount=type=cache,target=/root/.dotnet/tool \
|
||||
dotnet tool install --global dotnet-ef
|
||||
ENV PATH="${PATH}:/root/.dotnet/tools"
|
||||
ARG CONFIGURATION
|
||||
ARG PUBLISHED_PROJECT
|
||||
ARG DATABASE_PROJECT
|
||||
ARG DBCONTEXT
|
||||
ENV CONTEXT="${DBCONTEXT:+--context ${DBCONTEXT}}"
|
||||
ENV EFPROJECT=src/${DATABASE_PROJECT}/${DATABASE_PROJECT}.csproj
|
||||
ENV RUNNER=src/${PUBLISHED_PROJECT}/${PUBLISHED_PROJECT}.csproj
|
||||
|
||||
RUN --mount=type=cache,target=/root/.nuget/packages \
|
||||
dotnet ef migrations bundle --configuration ${CONFIGURATION} --no-build --project ${EFPROJECT} --startup-project ${RUNNER} --output /app/migrations ${CONTEXT}
|
||||
|
||||
FROM sdk AS migrations
|
||||
COPY --link --chown=root:root --chmod=755 --from=migration-bundle /app/migrations .
|
||||
USER app
|
||||
ENTRYPOINT ["./migrations"]
|
||||
CMD []
|
||||
|
||||
FROM alpine:latest AS pyroscope-agent
|
||||
ARG PYROSCOPE_VERSION
|
||||
RUN mkdir -p /opt/pyroscope && \
|
||||
if [ -n "${PYROSCOPE_VERSION}" ]; then \
|
||||
wget -qO- "https://github.com/grafana/pyroscope-dotnet/releases/download/v${PYROSCOPE_VERSION}-pyroscope/pyroscope.${PYROSCOPE_VERSION}-musl-x86_64.tar.gz" \
|
||||
| tar xz -C /opt/pyroscope; \
|
||||
fi
|
||||
|
||||
FROM ${RUNTIME_BASE} AS final
|
||||
ARG PUBLISHED_PROJECT
|
||||
ARG RUNTIME_BASE
|
||||
ENV LC_ALL=en_US.UTF-8 LANG=en_US.UTF-8
|
||||
|
||||
COPY --link --from=publish --chown=root:root /app/publish .
|
||||
COPY --link --from=pyroscope-agent --chown=root:root /opt/pyroscope/ /pyroscope/
|
||||
RUN if [ "${RUNTIME_BASE}" = "self-contained" ]; then \
|
||||
chmod +x ${PUBLISHED_PROJECT}; \
|
||||
printf '#!/bin/sh\nexec /app/%s "$@"\n' "${PUBLISHED_PROJECT}" > /app/entrypoint.sh; \
|
||||
else \
|
||||
printf '#!/bin/sh\nexec dotnet /app/%s.dll "$@"\n' "${PUBLISHED_PROJECT}" > /app/entrypoint.sh; \
|
||||
fi && chmod +x /app/entrypoint.sh
|
||||
|
||||
USER $APP_UID
|
||||
ENTRYPOINT ["/app/entrypoint.sh"]
|
||||
Reference in New Issue
Block a user