New in .NET 8.0 (38): Create container files via dotnet publish

0
20
New in .NET 8.0 (38): Create container files via dotnet publish


Since .NET 7.0 you can already use this dotnet publish Create a Docker container without first providing a Dockerfile (an additional text file).

Advertisement





Dr. Holger Schwittenberg is the technical director of the expert network www.IT-Visions.de, which supports numerous medium-sized and large companies through consulting and training as well as software development with 53 renowned experts. Through his appearances at numerous national and international conferences as well as over 90 expert books and over 1,500 expert articles, Holger Schwittenberg is one of the best-known experts for .NET and Web technologies in Germany.

The one with .NET 7.0 via options PublishProfile=DefaultContainer The built Docker container was automatically deployed in Docker as an active container.

There is an additional option in .NET 8.0: -ContainerArchiveOutputPath. This allows you to create a container file (tar.gz) without making it directly available as an active container:

Croview Note – a “notebook” not only for SBC developersCroview Note – a “notebook” not only for SBC developers

dotnet publish -p PublishProfile=DefaultContainer\
 -p ContainerArchiveOutputPath=t:\meinblazorimage.tar.gz\
 -p:ContainerBaseImage=mcr.microsoft.com/dotnet/aspnet:8.0-alpine




(Image: Dmytro Vikarchuk/Shutterstock)

In Online Conference BetterCode() .NET 9.0 On November 19, 2024 by iX and dpunkt.verlag, .NET experts from www.IT-Visions.de will present a ready-made version of .NET 9.0 using practical examples.

These include innovations in the .NET 9.0 SDK, C# 13.0, ASP.NET Core 9.0, Blazor 9.0, Windows Forms 9.0, WPF 9.0, WinUI, .NET MAUI 9.0, and the integration of artificial intelligence into .NET applications.

Program Offers six lectures, one discussion and six workshops. Tickets are available until October 22 Available at an introductory price,

The following code shows the complete example:

function New-TempDirectory {
    $parent = (System.IO.Path)::GetTempPath()
    (string) $name = (System.Guid)::NewGuid()
    New-Item -ItemType Directory -Path (Join-Path $parent $name)
}
 
docker --version
 
# Tempordner erzeugen und dahin wechseln
New-TempDirectory | cd
 
# Projekt anlegen (hier: Blazor SSR)
dotnet new blazor -n BlazorImContainer
 
# In den Ordner wechseln
cd .\BlazorImContainer\
 
#region Programmcode in Startseite austauschen mit Informationen über Umgebung, .NET- und OS-Version sowie Prozess
$indexpage = @'
@page "/"
Index

diese Blazor Web App (Blazor Static SSR) läuft @if (System.Environment.GetEnvironmentVariable("DOTNET_RUNNING_IN_CONTAINER")=="true") { im Container } else { nicht im Container }!

.NET-Version: @System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription
Betriebssystem: @System.Runtime.InteropServices.RuntimeInformation.OSDescription
Prozess: @System.Diagnostics.Process.GetCurrentProcess().ProcessName
Prozessidentität: @System.Environment.UserDomainName\@System.Environment.UserName
IsPrivilegedProcess: @System.Environment.IsPrivilegedProcess


Umgebungsvariablen:
    @foreach(System.Collections.DictionaryEntry e in System.Environment.GetEnvironmentVariables()) {
  • @e.Key: @e.Value
  • }
'@ $indexpage | Set-Content "components/pages/home.razor" #endregion # Container-Build-Paket hinzufügen (neu seit .NET 7.0, wird nicht mehr explizit gebraucht in .NET 8.0!!!) #dotnet add package Microsoft.NET.Build.Containers --prerelease # Veröffentlichen als Containerdatei (neu seit .NET 8.0) dotnet publish -p PublishProfile=DefaultContainer -p ContainerArchiveOutputPath=t:\meinblazorimage.tar.gz -p:ContainerBaseImage=mcr.microsoft.com/dotnet/aspnet:8.0-alpine # dann irgendwann: Laden in Docker docker load --input t:\meinblazorimage.tar.gz # Das ging schon in .NET 7.0 # Veröffentlichen als Container, alle MSBuild-Properties nach PublishProfile=DefaultContainer sind optional #dotnet publish --os linux --arch x64 -c Release -p:PublishProfile=DefaultContainer -p:ContainerImageName=meinblazorimage #-p:ContainerBaseImage=mcr.microsoft.com/dotnet/aspnet:7.0-alpine # Start des Containers (in getrennten Prozess, weil sonst dieser hier blockiert ist) Start-Process powershell { docker run -it --rm -p 5000:8080 blazorimcontainer } # optionaler Aufruf des Browsers zur Kontrolle Start-Process "http://localhost:5000"


(RME)

New in .NET 8.0 (38): Create container files via dotnet publishNew in .NET 8.0 (38): Create container files via dotnet publish

LEAVE A REPLY

Please enter your comment!
Please enter your name here