Rainer Stropek | @rstropek
# https://hub.docker.com/_/microsoft-dotnet
FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
WORKDIR /source
# copy csproj and restore as distinct layers
COPY aspnetapp/*.csproj .
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 mcr.microsoft.com/dotnet/aspnet:7.0
WORKDIR /app
COPY --from=build /app .
ENTRYPOINT ["dotnet", "aspnetapp.dll"]
Build
Runtime
# SLN folder is the current folder
dotnet publish --os linux --arch x64 -p:PublishProfile=DefaultContainer
docker images dotnet7api
docker tag <image-id> rstropek/dotnet7api
docker run -it --rm --name dotnet7api rstropek/dotnet7api
sudo apt install dotnet6
# Based on previous demo
docker images dotnet7api
# Note size
# Add to .csproj:
# <ContainerBaseImage>mcr.microsoft.com/dotnet/nightly/aspnet:7.0-jammy-chiseled</ContainerBaseImage>
# Rebuild
dotnet publish --os linux --arch x64 -p:PublishProfile=DefaultContainer
docker images dotnet7api
# Note size difference
docker run -it --rm --name dotnet7api <image-id>
var v = 42;
var c = new Container<int>(ref v);
Console.WriteLine(c.Value);
// Change v. As c has a reference to v, c.Value changes, too.
v = 43;
Console.WriteLine(c.Value);
ref struct Container<T>
{
ref T value;
public Container(ref T value)
{
this.value = ref value;
}
public T Value
{
get => this.value;
set => this.value = value;
}
public void ChangeRef(ref T value) => this.value = ref value;
}
var data = new[] { 2, 1, 3 };
var sorted = data.Order();
// instead of
// var sorted = data.OrderBy(e => e);
var sortedDesc = data.OrderByDescending();
// instead of
// var sortedDesc = data.OrderByDescending(e => e);
Rainer Stropek | @rstropek