In this post, let's see how we can create a containerized version of our application by doing just dotnet publish. Note: In order to do this, you’ll need .NET SDK 7.0.100, preview 7 or greater installed and currently only supported for Web Projects (Microsoft.NET.Sdk.Web).
Let's start with a simple example. Here I am creating a Blazor Server App using the default template.
dotnet new blazorserver -n hello-blazor-container cd .\hello-blazor-container\
Next, we need to install this new package: Microsoft.NET.Build.Containers. This is the package that lets us build container image from the project.
# add a reference to package that creates the container dotnet add package Microsoft.NET.Build.Containers
And now this is what our .csproj looks like.
<Project Sdk="Microsoft.NET.Sdk.Web"> <PropertyGroup> <TargetFramework>net7.0</TargetFramework> <Nullable>enable</Nullable> <ImplicitUsings>enable</ImplicitUsings> <RootNamespace>hello_blazor_container</RootNamespace> </PropertyGroup> <ItemGroup> <PackageReference Include="Microsoft.NET.Build.Containers" Version="0.1.8" /> </ItemGroup> </Project>
And that's mostly it. All we need to do now is running dotnet publish, setting PublishProfile=DefaultContainer.
# publish dotnet publish --os linux --arch x64 -p:PublishProfile=DefaultContainer
docker publish |
Alternatively, you can update the csproj file as follows and then you can just run dotnet publish without supplying any arguments.
<Project Sdk="Microsoft.NET.Sdk.Web"> <PropertyGroup> <TargetFramework>net7.0</TargetFramework> <Nullable>enable</Nullable> <ImplicitUsings>enable</ImplicitUsings> <RootNamespace>hello_blazor_container</RootNamespace> <RuntimeIdentifier>linux-x64</RuntimeIdentifier> <PublishProfile>DefaultContainer</PublishProfile> </PropertyGroup> <ItemGroup> <PackageReference Include="Microsoft.NET.Build.Containers" Version="0.1.8" /> </ItemGroup> </Project>
Now if we examine our local docker images, I can see the new image is created.
docker images |
Now let's create a container and run it.
# run the application as a container docker run -it --rm -p 5000:80 hello-blazor-container:1.0.0
docker run |
Now if I open up
http://localhost:5000, I
can see the application is up and running.
Running Container |
To learn more about this feature, read:
Happy Coding.
Regards,
Jaliya
No comments:
Post a Comment