DEV Community

Cover image for An Introduction to the World of Containers with .NET 8
James Montemagno for .NET

Posted on

An Introduction to the World of Containers with .NET 8

Containers are a popular way of packaging and distributing applications in today’s Cloud Native landscape – but what are they, and how can .NET developers integrate them into their workflows? Today, let's talk about what containers are, how they relate to Docker, and how the .NET tooling makes it easy for developers to easily streamline the process of creating containers with .NET.

Containers with .NET For Beginners

Follow along with this blog and our new Beginner's series on containers and Docker for .NET.

What are Containers?

A container is a standard unit of software that packages up code and all its dependencies. It allows applications to run consistently across different computing environments. You'll find containers supported by all major cloud providers with various ways to run your containers, ranging from serverless options to fully managed Kubernetes services.

Containers provide a flexible choice for application packaging, making it a valuable skill to master. That's why .NET invests in making containers easy to use for developers.

The Benefits of Containers

So why should you use containers? The answer lies in a uniform deployment and uniform execution of applications. Containers make the job of maintaining and operating your services much easier in the long term. In addition, the whole world of automated DevOps, tooling, alerting, and monitoring can be integrated into your solution.

Developer Setup

The first step is to install the .NET SDK. You can use the .NET CLI to create containers which by default creates Linux containers. You can create these containers on any operating system; Windows, Mac, Linux, or the Windows Subsystem for Linux. The choice is yours however you enjoy developing. Additionally, having Docker Desktop installed will enable you to run your containers after you create them with the .NET SDK.

Making Containers with .NET

Now, let's take a look at just how easy it can be to create containers with .NET. To start, let's first create a new web app with the following .NET CLI command:

dotnet new web -o container-webapp
Enter fullscreen mode Exit fullscreen mode

Then we can navigate to the new directory that it was created in:

cd container-webapp
Enter fullscreen mode Exit fullscreen mode

You can now run the application locally with dotnet run, but let's go ahead and create a container by publishing the app with the following command:

dotnet publish -t:PublishContainer
Enter fullscreen mode Exit fullscreen mode

By running a single command, you can create a container of your web app. This is the same publish command you're already familiar with for publishing your web application, but now it's being used for containerization. The .NET SDK will provide you with information about the container it just created, such as the container name, the tag, and the base image it chose.

Speaking of the base image, it's like the operating system and software configuration that your application will be deployed onto. In this example, as a web app targeting .NET 8, the SDK chose an ASP.NET image from Microsoft with the 8.0 tag. You will see output from the command similar to the following that highlights this:

Building image 'conainer-webapp' with tag 'latest' on top of the base image ''.
Pushed image 'conainer-webapp:latest' to local registry via 'docker'.
Enter fullscreen mode Exit fullscreen mode

To get a closer look at the image we just built, we can use Docker Desktop. It's fascinating to see how the image is composed of different layers, each representing a portion of the file system or a change to the operating environment. These layers come together to create a runnable container.

Layer of the container in Docker Desktop

Let's run the container with a docker run command that specifies the container name:

docker run -P container-webapp
Enter fullscreen mode Exit fullscreen mode

Optionally, we could run the container directly from Docker Desktop instead of using the command. If we open Docker Desktop, we can access our running application by clicking on the port link next to our container:

Image description

And there you have it, a browser will open up with our new web app running in a container!

Image description

Next Steps: Deep Dive into Tooling & Publishing to the cloud

That wraps up our quick taste of containers and how .NET makes it easy to get started with them. This is only the first step, and I have a full beginner's series that dives deeper into the tooling experiences available on the command line as well as in Visual Studio and Visual Studio Code. Then I go into how you can take your containers and publish them to container registries. In addition, we have full training on Microsoft Learn for containers, microservices, and cloud-native development with .NET that I highly recommend you checkout.

Thanks for joining me on this quick introduction to containers with .NET.

AI-assisted content. This article was partially created with the help of AI. An author reviewed and revised the content as needed.

Top comments (1)

jangelodev profile image
João Angelo

Hi James Montemagno ,
Your tips are very useful
Thanks for sharing