Skip to content

Instantly share code, notes, and snippets.

@FilBot3
Last active April 29, 2022 19:56
Show Gist options
  • Select an option

  • Save FilBot3/95536ee509bcf6c366ffa9e1cf6dcddf to your computer and use it in GitHub Desktop.

Select an option

Save FilBot3/95536ee509bcf6c366ffa9e1cf6dcddf to your computer and use it in GitHub Desktop.
.NET Core ASP.Net Core universal containerfile.
# vim: ft=dockerfile
# To build this Container Image, you'll need to use the following syntax.
#
# podman build --file=DotNet.containerfile --tag=aspwebapi:latest --build-arg="ASP_EXE=/app/MvcMovie" .
#
# What this will do is use the specified Containerfile, then assign the given
# tag. Then it will pass the build argument of ASP_EXE. This argument is then
# passed the containerfile as an ENV and substituted in a shell script
# entrypoint. This makes it so the Containerfile is universal, atleast for
# DotNet Core and .NET 6.
#
# @see https://hub.docker.com/_/microsoft-dotnet
# Set the version of .NET being used. This should be common across both the SDK
# and the ASP.Net runtime.
ARG DOTNET_VERSION=6.0
# Pulling the images from Microsoft during the build.
# This is a multi-stage container build. This stage is called build.
# @see https://hub.docker.com/_/microsoft-dotnet-sdk/
FROM mcr.microsoft.com/dotnet/sdk:${DOTNET_VERSION} as build
# Set the current working directory to /build
WORKDIR /build
# Copy over the whole project to the WORKDIR.
COPY . .
# Restore the NuGet packages.
RUN dotnet restore
# Build the project.
RUN dotnet build
# Test the code.
RUN dotnet test
# Publish the built code.
RUN dotnet publish --output dist --no-build
# Stage 2, copy the published code to the ASP.Net runtime container.
# @see https://hub.docker.com/_/microsoft-dotnet-aspnet/
FROM mcr.microsoft.com/dotnet/aspnet:${DOTNET_VERSION}
# Build Argument for the name of the ASP.Net Executable to run.
# If left default, the container will print hostname.
ARG ASP_EXE=hostname
ENV ASP_EXE=${ASP_EXE}
# Set our default listening port to 8080 so it's non-administrative.
ENV ASPNETCORE_URLS=http://+:8080
# Set the working directory to /app.
WORKDIR /app
# Copy from the build stage to the current stage's /app directory.
COPY --from=build /build/dist /app
# This will generate the container_entrypoint.sh
# script that is used to run the container.
# #!/usr/bin/env bash
#
# if [[ -z $ASP_EXE ]]; then
# echo "No App Provided"
# exit 1
# fi
# exec "${ASP_EXE}" $@
RUN echo IyEvdXNyL2Jpbi9lbnYgYmFzaAoKaWYgW1sgLXogJEFTUF9FWEUgXV07IHRoZW4KICBlY2hvICJObyBBcHAgUHJvdmlkZWQiCiAgZXhpdCAxCmZpCgpleGVjICIke0FTUF9FWEV9IiAkQAo= \
| base64 --decode \
| tee /container_entrypoint.sh && \
chmod +x /container_entrypoint.sh
# Now we'll directly call the executable that is generated.
ENTRYPOINT ["/container_entrypoint.sh"]
# This LABEL allows users of Podman to use the podman container runlabel command.
# This would look like:
#
# podman container runlabel run localhost/aspwebapi:latest
#
# Then it will perform the command defined in the RUN label. Many other LABELs
# can be defined as well and used to make running the container a lot easier.
LABEL RUN="podman run --rm --name=\"aspwebapp\" --publish=\"8080:8080\" IMAGE"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment