Skip to content

Instantly share code, notes, and snippets.

@lbussell
Created March 28, 2023 20:49
Show Gist options
  • Save lbussell/45f157e1f7beaec0f3e073793a52b1f3 to your computer and use it in GitHub Desktop.
Save lbussell/45f157e1f7beaec0f3e073793a52b1f3 to your computer and use it in GitHub Desktop.
aspnet composite playground
################################################################################
# runtime-deps
################################################################################
FROM amd64/debian:bookworm-slim as runtime-deps
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
ca-certificates \
\
# .NET dependencies
libc6 \
libgcc1 \
libgssapi-krb5-2 \
libicu72 \
libssl3 \
libstdc++6 \
zlib1g \
&& rm -rf /var/lib/apt/lists/*
# Create a non-root user and group
RUN groupadd \
--system \
--gid=64198 \
app \
&& useradd \
--uid 64198 \
--gid 64198 \
--create-home \
--system \
app
ENV \
# Configure web servers to bind to port 8080 when present
ASPNETCORE_HTTP_PORTS=8080 \
# Enable detection of running in a container
DOTNET_RUNNING_IN_CONTAINER=true
################################################################################
# runtime
################################################################################
# Installer image
FROM amd64/buildpack-deps:bookworm-curl AS runtime-installer
# Retrieve .NET Runtime
RUN dotnet_version=8.0.0-preview.3.23170.2 \
&& curl -fSL --output dotnet.tar.gz https://dotnetbuilds.azureedge.net/public/Runtime/$dotnet_version/dotnet-runtime-$dotnet_version-linux-x64.tar.gz \
&& dotnet_sha512='40509affecc0e63f88f4ea0c00bcd8bcbff2730dd1605cc487ffc212db63e6e27eca0bccb4a160f98e11cbf847562e9d4e9cb18b76f3555e2eddcc7532952ea9' \
&& echo "$dotnet_sha512 dotnet.tar.gz" | sha512sum -c - \
&& mkdir -p /dotnet \
&& tar -oxzf dotnet.tar.gz -C /dotnet \
&& rm dotnet.tar.gz
# .NET runtime image
FROM runtime-deps as runtime
# .NET Runtime version
ENV DOTNET_VERSION=8.0.0-preview.3.23170.2
COPY --from=runtime-installer ["/dotnet", "/usr/share/dotnet"]
RUN ln -s /usr/share/dotnet/dotnet /usr/bin/dotnet
################################################################################
# aspnet
################################################################################
ARG REPO=mcr.microsoft.com/dotnet/runtime
# Installer image
FROM amd64/buildpack-deps:bookworm-curl AS aspnetcore-installer
# Retrieve ASP.NET Core
RUN aspnetcore_version=8.0.0-preview.3.23170.14 \
&& curl -fSL --output aspnetcore.tar.gz https://dotnetbuilds.azureedge.net/public/aspnetcore/Runtime/$aspnetcore_version/aspnetcore-runtime-$aspnetcore_version-linux-x64.tar.gz \
&& aspnetcore_sha512='81ccca780dc98e1536743510489ec47d198b38da9b434e0dc73b6782b758d4d57bc778cc6aaf39a1b1b13c32bb8d6c59deb8c1e0ff20a240a3d7a80d03a396a8' \
&& echo "$aspnetcore_sha512 aspnetcore.tar.gz" | sha512sum -c - \
&& tar -oxzf aspnetcore.tar.gz ./shared/Microsoft.AspNetCore.App \
&& rm aspnetcore.tar.gz
# ASP.NET Core image
FROM aspnetcore-installer as aspnetcore
# ASP.NET Core version
ENV ASPNET_VERSION=8.0.0-preview.3.23170.14
COPY --from=aspnetcore-installer ["/shared/Microsoft.AspNetCore.App", "/usr/share/dotnet/shared/Microsoft.AspNetCore.App"]
################################################################################
# aspnet composite
################################################################################
# Installer image
FROM amd64/buildpack-deps:bookworm-curl AS aspnet-composite-installer
# Retrieve .NET Runtime
RUN aspnetcore_version=8.0.0-preview.3.23170.14 \
&& curl -fSL --output dotnet.tar.gz https://dotnetbuilds.azureedge.net/public/aspnetcore/Runtime/$aspnetcore_version/aspnetcore-runtime-composite-$aspnetcore_version-linux-x64.tar.gz \
&& dotnet_sha512='06a24a0992645757e471d6bd6244328e9f0a352ad198697c97136c997f5bfcc6b947f2e896dc3bd69590781a1befc0ec5531859b4e25456b97a24320bcacd42a' \
&& echo "$dotnet_sha512 dotnet.tar.gz" | sha512sum -c - \
&& mkdir -p /dotnet \
&& tar -oxzf dotnet.tar.gz -C /dotnet \
&& rm dotnet.tar.gz
FROM runtime-deps as aspnet-composite
# .NET Runtime version
ENV DOTNET_VERSION=8.0.0-preview.3.23170.2
# ASP.NET Core version
ENV ASPNET_VERSION=8.0.0-preview.3.23170.14
COPY --from=aspnet-composite-installer ["/dotnet", "/usr/share/dotnet"]
RUN ln -s /usr/share/dotnet/dotnet /usr/bin/dotnet
################################################################################
# sdk on aspnet composite
################################################################################
FROM aspnet-composite as sdk-on-aspnet-composite
ENV \
# Do not generate certificate
DOTNET_GENERATE_ASPNET_CERTIFICATE=false \
# Do not show first run text
DOTNET_NOLOGO=true \
# SDK version
DOTNET_SDK_VERSION=8.0.100-preview.3.23173.11 \
# Enable correct mode for dotnet watch (only mode supported in a container)
DOTNET_USE_POLLING_FILE_WATCHER=true \
# Skip extraction of XML docs - generally not useful within an image/container - helps performance
NUGET_XMLDOC_MODE=skip \
# PowerShell telemetry for docker image usage
POWERSHELL_DISTRIBUTION_CHANNEL=PSDocker-DotnetSDK-Debian-12
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
curl \
git \
wget \
&& rm -rf /var/lib/apt/lists/*
# Install .NET SDK
RUN curl -fSL --output dotnet.tar.gz https://dotnetbuilds.azureedge.net/public/Sdk/$DOTNET_SDK_VERSION/dotnet-sdk-$DOTNET_SDK_VERSION-linux-x64.tar.gz \
&& dotnet_sha512='cc3bb3b9f83554d83b6cad58ad1b15afae14bed6866774777f77f4d7f5e2776c5adaac5c694af0f7223e91fe13f53d72153b0d07500c9335d9f6d0c4147d0745' \
&& echo "$dotnet_sha512 dotnet.tar.gz" | sha512sum -c - \
&& mkdir -p /usr/share/dotnet \
&& tar -oxzf dotnet.tar.gz -C /usr/share/dotnet ./packs ./sdk ./sdk-manifests ./templates ./LICENSE.txt ./ThirdPartyNotices.txt \
&& rm dotnet.tar.gz \
# Trigger first run experience by running arbitrary cmd
&& dotnet help
################################################################################
# sdk on runtime
################################################################################
FROM runtime as sdk-on-runtime
ENV \
# Do not generate certificate
DOTNET_GENERATE_ASPNET_CERTIFICATE=false \
# Do not show first run text
DOTNET_NOLOGO=true \
# SDK version
DOTNET_SDK_VERSION=8.0.100-preview.3.23173.11 \
# Enable correct mode for dotnet watch (only mode supported in a container)
DOTNET_USE_POLLING_FILE_WATCHER=true \
# Skip extraction of XML docs - generally not useful within an image/container - helps performance
NUGET_XMLDOC_MODE=skip \
# PowerShell telemetry for docker image usage
POWERSHELL_DISTRIBUTION_CHANNEL=PSDocker-DotnetSDK-Debian-12
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
curl \
git \
wget \
&& rm -rf /var/lib/apt/lists/*
# Install .NET SDK
RUN curl -fSL --output dotnet.tar.gz https://dotnetbuilds.azureedge.net/public/Sdk/$DOTNET_SDK_VERSION/dotnet-sdk-$DOTNET_SDK_VERSION-linux-x64.tar.gz \
&& dotnet_sha512='cc3bb3b9f83554d83b6cad58ad1b15afae14bed6866774777f77f4d7f5e2776c5adaac5c694af0f7223e91fe13f53d72153b0d07500c9335d9f6d0c4147d0745' \
&& echo "$dotnet_sha512 dotnet.tar.gz" | sha512sum -c - \
&& mkdir -p /usr/share/dotnet \
&& tar -oxzf dotnet.tar.gz -C /usr/share/dotnet ./packs ./sdk ./sdk-manifests ./templates ./LICENSE.txt ./ThirdPartyNotices.txt ./shared/Microsoft.AspNetCore.App \
&& rm dotnet.tar.gz \
# Trigger first run experience by running arbitrary cmd
&& dotnet help
################################################################################
# sample
################################################################################
# https://hub.docker.com/_/microsoft-dotnet
FROM sdk-on-runtime AS build
WORKDIR /source
# copy csproj and restore as distinct layers
COPY aspnetapp/*.csproj .
COPY aspnetapp/NuGet.config.nightly ./NuGet.config
RUN dotnet restore --use-current-runtime
# copy everything else and build app
COPY aspnetapp/. .
RUN dotnet publish -c Release -o /app --use-current-runtime --self-contained false --no-restore
# final stage/image
FROM aspnet-composite as aspnetapp
WORKDIR /app
COPY --from=build /app .
ENTRYPOINT ["dotnet", "aspnetapp.dll"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment