18 June 2023

.NET Core - Standard Dockerfile for ASP Core

Dockerfile

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /source

# copy csproj and restore as distinct layers
COPY *.csproj .
RUN dotnet restore

# copy and publish app and libraries
COPY . .
RUN dotnet publish -c release -o /app

# final stage/image
FROM mcr.microsoft.com/dotnet/aspnet:6.0

# Set timezone
ENV TZ=Australia/Adelaide
ENV LANG en_AU.UTF-8
ENV LANGUAGE ${LANG}
ENV LC_ALL ${LANG}

WORKDIR /app
COPY --from=build /app .
# RUN ["/bin/bash"]
ENTRYPOINT ["dotnet", "<projectName>.dll"]

Replace the .dll in the ENTRYPOINT with the name of your project.

Use aspnet for ASP Core app’s and the runtime for others.

FROM mcr.microsoft.com/dotnet/runtime:6.0

Remove the timezone information to use UTC.

You can use a script bash similar to the following to build and deploy on a remote host.

export DOCKER_HOST=ssh://<remote_host>
docker build -t <image_name> .
docker stop <container_name>
docker rm <container_name>
docker run -p 9090:80 -d --restart unless-stopped --name <container_name> <image_name>
tags: c# - garbage collector