Home > Dockerfile for .NET 8 technical stack on Ubuntu agents
Wafaa TOUNZI
9 September 2024
Lire cet article en Français

Dockerfile for .NET 8 technical stack on Ubuntu agents

Dockerfile for .NET 8 technical stack on Ubuntu agents

The importance of containers lies in their efficiency to package up a developer’s application with all its libraries and dependencies to ship it all out as one package. But with the arrival of .NET8, there have been some specific commands that need to be added in the Dockerfile to be able to containerize the application under an Ubuntu agent in case of an installation failure. So, in this article we will dive into many subjects:

  • Understanding the Nuances of .NET 8 for Docker.
  • The interaction between Azure DevOps and Azure Container Registry.
  • The basics of a Dockerfile.
  • The necessary commands for a .NET 8 application’s Dockerfile and its CI pipeline task.

 

Understanding the Nuances of .NET 8 for Docker

The launch of .NET 8 brings with it a series of significant updates and changes indeed. But this iteration presents a unique challenge for the dotnet-sdk-8.0 which is a crucial component for development. Unfortunately, it is not pre-available in the official Linux packages. That is why Devs are required to add extra steps to manually integrate this SDK into their environments. Consequently, this process involves more than the usual procedures, demanding specific commands to successfully incorporate dotnet-sdk-8.0 into Docker images, thereby ensuring that apps can fully exploit the capabilities of .NET 8.

 

How Does Azure DevOps and Azure Container Registry Interact?

 

Azure DevOps as we all know is a platform for continuous integration and delivery where, in this context, pipelines are configured to execute tasks that build and push Docker images to Azure Container Registry (ACR), which is an azure managed service for storing and managing Docker container images.

Azure DevOps et Azure Container Registry

 

The interaction process

 

Developer: The process begins with the developer committing his code and the Dockerfile to Azure Repos.

Azure Pipelines (Build & Push): The build step uses the Dockerfile to create a Docker image, this is done by executing the docker build commands. Once the image is built, the same Azure Pipelines process pushes the image to Azure Container Registry (ACR) using the docker push command. The access from Azure DevOps platforms to Azure Cloud environment is done using service connections, making it secure and straightforward.

Azure Container Registry: The Docker image is now stored in the ACR. From here, the image can be deployed to many Azure services like Azure Kubernetes Service (AKS), Azure Web Apps for Containers, or any other resource that can pull container images from ACR.

 

Dockerfile Basics

 

A Dockerfile is a text document that contains all the commands a user usually needs to execute in succession on his command line, in order to create an automated build to assemble an image.

 

Key Concepts of a Dockerfile:

 

FROM: Specifies the base image from which you are building.

RUN: Executes a command in the container.

COPY: Copies files or directories from the host file system to the container.

WORKDIR: Sets the working directory for any RUN, CMD, ENTRYPOINT, COPY, and ADD instructions that follow in the Dockerfile.

ARG: Defines a variable that users can pass at build-time.

ENTRYPOINT: Configures a container that will run as an executable.

CMD: the default command to run when a container launched from the Docker image.

A very important point to add is that multi-stage builds in Docker are a game-changer. By separating the build environment from the runtime environment, we make everything neater and safer. Plus, it helps move the app from building it to running it without any mess.

Here’s some of the multi-stage build benefits:

  • Images end up smaller: this means your app only has the essentials it needs to work, nothing extra.
  • More secure containers: packages and dependencies need to be kept up to date because they can be a potential source of vulnerability for attackers to exploit. Using Docker multi-stage build means the resulting container will be more secure because your final image includes only what it needs to run the application.
  • Faster deployment: This means you can build, test, and launch your app faster than before, and it runs smoother too.

 

Dockerfile Solution for .NET 8 Environment on Ubuntu

 

If your get these log errors about a failing installation or missing SDK packages, then make sure you follow the solution I provided:

ERROR: failed to solve: process “/bin/sh -c apt-get update && apt install -y dotnet-sdk-8.0” did not complete successfully
ERROR: failed to solve: process “/bin/sh -c apt-get update && apt install -y aspnetcore-runtime-8.0” did not complete successfully

Ubuntu Code Docker

 

 

 

 

 

To set up a .NET 8 environment on an Ubuntu agent, specific commands are required to install the SDK missing packages. We will proceed to explain those command lines below.

 

The three main commands that are needed to be executed after the image base call are:

  • RUN apt-get update && apt-get install -y wget: This command updates the list of packages and their versions on your Ubuntu system. Then it installs the wget utility, which is used here to download additional files, such as package repositories, directly from the command line.
  • RUN wget https://packages.microsoft.com/config/ubuntu/22.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb: This command uses wget to download a .deb package from Microsoft’s package repository.
  • RUN dpkg -i packages-microsoft-prod.deb: the dpkg -i command is used to install a .deb The output we had “packages-microsoft-prod.deb” adds Microsoft’s package repository to the list of sources for apt which is the package manager for our Ubuntu.

The rest of the Dockerfile have the application’s setup up that would restore dependencies, build and publish the application respectively. After that, comes the runtime image creation and setup that uses the published output in the build step. Lastly, we would have the configuration and execution steps to listen on a particular port number as well as specifying an entry point to start the application.

 

Setting Up the CI Pipeline with Azure DevOps

 

Here’s a simple example of the adequate CI Pipeline task (Docker@2) that would login, build and push our Docker image with a tag (BuildID):

 

P.S: $(DOCKER_IMAGE_NAME) and $(Build.BuildId) variables are used to name and tag the image, ensuring that each build is unique and traceable to a specific build ID. Alternatively, you have the option to utilize a Git tag version, such as vx.x.x, for tagging instead of relying on BuildId.

 

Key Takeaways

 

To sum up, in the context of setting up a Dockerfile for .NET 8 applications and operating the build through Azure DevOps, the three commands we explained above are crucial since they ensure the Ubuntu-based Docker image is equipped with the necessary tools.

Would you like to learn more or get some expert help on your Azure Projects? Contact us!

 

This posts should interest you
Comments
Leave a Reply

Receive the best of Cloud, DevOps and IT news.
Receive the best of Cloud, DevOps and IT news.