40
loading...
This website collects cookies to deliver better user experience
This articles focuses on building the app inside a Linux container.
I won't talk about .NET Framework (4.x) which would require the Windows platform to run.
FROM mcr.microsoft.com/dotnet/sdk:5.0-alpine AS build
WORKDIR /source
# Copy and restore
COPY . .
# make sure this is set in the RuntimeIdentifiers of the .csproj
RUN dotnet restore -r linux-musl-x64
# publish project (StartupProject is the startup project)
RUN dotnet publish -c Release -o /appReleased -r linux-musl-x64 \
--no-restore StartupProject/*.csproj
ENTRYPOINT ["/appReleased/StartupProject"]
appsettings.json
. Nevertheless there are a bunch more configuration providers pre-built in the ASP .NET Core 5.0 framework. These include the following:appsettings.json
file and environment variables.__
(a double underline
).// appsettings.json
{
"JWT": {
"Secret": ""
}
// ...
}
export JWT__Secret=superSecureJWTSecret
appsettings.json
, when the same key is provided. RUN dotnet test --no-restore
--no-restore
flag.Swashbuckle.AspNetCore
dotnet tool install --version 6.1.1 Swashbuckle.AspNetCore.Cli
⚠️ Make sure you use the same version of the CLI tool
and the NuGet, otherwise it will result in some strange error.
dotnet-tools
config present, which stores all installed tools and corresponding versions. This is important as this file is required for the build process later on. Make sure to also include it in your Git repository.# build swagger.json from controller/endpoint definitions
RUN dotnet tool restore \
dotnet swagger tofile --output swagger.json StartupProject/bin/Debug/net5.0/StartupProject.dll v1
Official Docker docs on multi-stage builds
self-contained
as publishing type is, to make sure that the correct RuntimeIdentifier
in your .csproj
file is set.<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<RuntimeIdentifiers>linux-musl-x64</RuntimeIdentifiers>
<OutputType>Exe</OutputType>
</PropertyGroup>
</Project>
linux-musl-x64
has to be added. You may enable multiple RuntimeIdentifers (seperated by ";") in order to make it executable also on other platforms as self-contained build (for example win-x64
).mcr.microsoft.com/dotnet/runtime-deps:5.0-alpine-amd64
includes everything just to run self-contained
apps.# Build stage
FROM mcr.microsoft.com/dotnet/sdk:5.0-alpine AS build
WORKDIR /source
# Copy csproj and restore as distinct layers
COPY . .
RUN dotnet restore -r linux-musl-x64
RUN dotnet test --no-restore
# build swagger.json from controller/endpoint definitions
RUN dotnet tool restore \
dotnet swagger tofile --output swagger.json StartupProject/bin/Debug/net5.0/StartupProject.dll v1
# publish project (StartupProject is the startup project)
RUN dotnet publish -c Release -o /appReleased -r linux-musl-x64 \
--no-restore StartupProject/*.csproj
# Create final image
FROM mcr.microsoft.com/dotnet/runtime-deps:5.0-alpine-amd64
COPY --from=build /appReleased /app
ENTRYPOINT ["/app/StartupProject"]
root
user, but there are multiple drawdowns when using the superuser with Docker.You can follow along here for details: https://sysdig.com/blog/dockerfile-best-practices/
# (truncated)
# Add user, group, and change ownership
RUN adduser --disabled-password --gecos "" --home /app --no-create-home --uid 10001 appuser \
# make sure to allow your user to access your application binary
&& chown -R appuser:appuser /app
USER appuser
# (truncated)
You can view the base Dockerfile here
# ...
ENV \
# Configure web servers to bind to port 80 when present
ASPNETCORE_URLS=http://+:80
# ...
# Set listening host and port
ENV ASPNETCORE_URLS=http://+:5000
EXPOSE 5000
# Build stage
FROM mcr.microsoft.com/dotnet/sdk:5.0-alpine AS build
WORKDIR /source
# Copy csproj and restore as distinct layers
COPY . .
RUN dotnet restore -r linux-musl-x64
RUN dotnet test --no-restore
# build swagger.json from controller/endpoint definitions
RUN dotnet tool restore \
dotnet swagger tofile --output swagger.json StartupProject/bin/Debug/net5.0/StartupProject.dll v1
# release project (StartupProject is the startup project)
RUN dotnet publish -c Release -o /appReleased -r linux-musl-x64 --self-contained true \
--no-restore /p:PublishTrimmed=true /p:PublishReadyToRun=true StartupProject/*.csproj
# Create final image
FROM mcr.microsoft.com/dotnet/runtime-deps:5.0-alpine-amd64
COPY --from=build /appReleased /app
# Add user, group, and change ownership
RUN adduser --disabled-password --gecos "" --home /app --no-create-home --uid 10001 appuser \
# make sure to allow your user to access your application binary
&& chown -R appuser:appuser /app
USER appuser
# Set listening host and port
ENV ASPNETCORE_URLS=http://+:5000
EXPOSE 5000
ENTRYPOINT ["/app/StartupProject"]
40