Monday, July 22, 2024
HomeC#No extra Dockerfiles with the assistance of .NET CLI –

No extra Dockerfiles with the assistance of .NET CLI –

The brand new .NET SDK helps creating Docker photos with out utilizing Dockerfiles – simply through the use of the .NET CLI – with .NET 7. At the moment, it’s only a preview and a few options will not be but obtainable, however I needed to attempt it anyway.

This text reveals what I needed to change with an current ASP.NET Core Internet API venture that’s deployed to Azure Container Apps utilizing GitHub actions.

Container in Clouds

Eradicating the Dockerfile

The ASP.NET Core utility already comprises a beforehand created Dockerfile as proven within the subsequent picture. The Dockerfile makes use of a multi-stage construct course of to construct the appliance. For constructing the appliance, the bottom picture used is which incorporates the .NET SDK. As a result of the online utility references some libraries, the libraries have to be constructed as effectively. After utilizing the CLI command dotnet restore to revive the NuGet packages, unit checks run with dotnet take a look at, and the appliance is constructed with dotnet construct.

> When creating the Dockerfile, I made a decision to run the unit checks straight from throughout the Dockerfile. This fashion it was not required to put in the preview model of the .NET SDK with the construct agent, as this model of the SDK was already obtainable with the bottom picture.

In one other stage, the recordsdata wanted for publishing are created with dotnet publish. The ultimate stage makes use of the bottom picture which incorporates the ASP.NET Core runtime.


Now I’m simply eliminating this Dockerfile.

> Some months in the past I had some work creating this Dockerfile. With the subsequent venture I can spare this time. One situation why eradicating the Dockerfile helps is defined within the article hyperlink under (Saying built-in container assist for the .NET SDK) describing the Docker construct context. I all the time needed to specify the context explicitly when constructing the picture as a result of some configuration recordsdata are in a base listing from the placement of the Dockerfile.

As an alternative of utilizing a Dockerfile, the NuGet bundle Microsoft.NET.Construct.Containers must be added.

Utilizing the dotnet publish enhancements

To assist the dotnet publish command, the venture file might be modified to incorporate the weather RuntimeIdentifier and PublishProfile:

Project file

To regionally construct the appliance, I simply used dotnet publish. If the RuntimeIdentifier and PublishProfile components will not be included, these values might be provided as arguments to the dotnet publish command:

dotnet publish CodeBreaker.APIs.sln --os linux --arch x64 -p:PublishContainer=DefaultContainer,ContainerImageName=codebreakerapi

Simply utilizing dotnet publish, the picture is constructed – with out making a Dockerfile, with out specifying the bottom photos. With ASP.NET Core functions, the bottom picture used is with the tag of the .NET model used.

These are the photographs used routinely:

  • ASP.NET Core apps:
  • Self-contained apps:
  • Different .NET apps:

By default, the Debian distribution is used. To specify a special base picture, the picture might be specified the the ContainerBaseImage ingredient:

The default title of the picture created makes use of the AssemblyName. This title is routinely transformed to lowercase as required

Publishing with GitHub Actions

To routinely construct and publish the appliance to Azure Container Apps, I’m utilizing GitHub Actions.

Earlier Model utilizing the Docker CLI

The following picture reveals the earlier model I used to be utilizing:

  • docker/setup-buildx-action to setup docker
  • docker/login-action to login to the Azure Container Registry (ACR)
  • docker/build-push-action to construct the Docker picture, and to publish the picture to ACR.

With the build-push-action I specified the tags, the Dockerfile, and the Docker context, and set the push configuration to push the picture after constructing it.

Publish to ACR with Docker Commands

New Model utilizing the .NET CLI

With the up to date model, the Docker instructions might be changed through the use of the .NET CLI.

  • actions/setup-dotnet to setup .NET – .NET beforehand was solely used throughout the Dockerfile
  • dotnet take a look at to run the unit checks, which was finished throughout the Dockerfile beforehand
  • dotnet publish to construct the Docker picture

In a future model, dotnet publish is perhaps the final command wanted to publish the picture to the ACR. Nevertheless, presently dotnet publish doesn’t assist authenticating with a container registry. That’s why some docker instructions are nonetheless used:

  • actions-hub/docker/login to login to ACR
  • actions-hub/docker/cli with the argument tag to tag the picture for publishing it to the repository
  • actions-hub/docker/cli with the argument push to push the picture to the repository

> Authentication is a crucial function; this enhancement is deliberate for .NET 7 RC1 (see the hyperlink under).

Use dotnet publish from GitHub Action

With the earlier model of the GitHub Motion, I didn’t use the .NET CLI in any respect – simply the Docker instructions. Now, .NET is configured with the actions/setup-dotnet motion.

Deploying the picture from ACR to Azure Container Apps was not modified between the earlier model and the up to date model. I left this code within the photos to indicate how this works.

Take away

The .NET CLI is the command that can be utilized to create, construct, take a look at, and publish .NET functions. Having options for constructing containers built-in to this CLI reduces complexity and makes it simpler to make use of for .NET builders.

Take pleasure in studying and programming!


Thanks to your assist of my weblog by shopping for a espresso. Throughout these months all of the coffees (and extra) shall be used to assist the Ukraine.



Please enter your comment!
Please enter your name here

Most Popular

Recent Comments