Iron Academy Logo
Learn C#

Containerize C# Application Easily with the .NET CLI

Tim Corey
9m 57s

Containers are a powerful tool, but for many developers, the idea of putting a C# application into a container can feel overwhelming. Setting up a Dockerfile and handling dependencies manually can be complicated. However, as Tim Corey explains in his video, the .NET CLI simplifies this process, allowing developers to containerize their applications without needing a Dockerfile.

In this article, we’ll take a deeper look at containerizing a C# application using Tim’s step-by-step approach from his video, "Containerize Your C# Application Easily with the .NET CLI." By following Tim’s guidance, we’ll understand how to publish a .NET application into a Docker container effortlessly.

Introduction to .NET CLI for Containers

At the beginning of the video, Tim explains the need for containerization. He highlights that while containers are a great tool, the process of setting up a Dockerfile and configuring it correctly can be complex.

Instead of manually creating a Dockerfile, Tim introduces an easier approach using the .NET CLI. This method allows developers to create a containerized application without writing a Dockerfile manually.

Tim also clarifies that this video is part of his 10-minute training series, which focuses on quick and effective introductions to technical topics.

The Application Setup

Tim begins by using a minimal Web API application as an example. The application contains a single endpoint called /messages, which returns some data.

To demonstrate the application’s behavior before containerization, Tim runs it locally. By accessing the /messages endpoint, he confirms that it works correctly outside a container.

Before proceeding with containerization, he also checks that:

  • Docker is installed and running on his machine.

  • There are no existing Docker images or containers at the start.

This ensures that when a new container is created, we can clearly see the changes.

Preparing for Containerization

To containerize the application, Tim moves to the command line within the project directory. He then runs the following command:

dotnet publish -o:Linux -a:x64 -p:PublishProfile=DefaultContainer -c:Release
dotnet publish -o:Linux -a:x64 -p:PublishProfile=DefaultContainer -c:Release
TODO
$vbLabelText   $csharpLabel

Tim breaks down each part of the command:

  • -o:Linux → Publishes the application for a Linux-based container.

  • -a:x64 → Specifies the architecture as 64-bit.

  • -p:PublishProfile=DefaultContainer → Uses the default container settings.

  • -c:Release → Publishes the app in release mode for better performance.

Tim emphasizes that Linux containers are preferred over Windows containers because Windows containers mainly serve as a bridge for .NET Framework applications.

Publishing and Creating a Container

Once the publish command is executed, Tim explains that the .NET CLI will automatically determine the necessary configurations and package everything into a container. He points out that:

  • There is no need for a Dockerfile in this method.

  • The process builds the application and containerizes it in one step.

  • The base image used is Microsoft’s .NET 8 runtime container.

After publishing, he checks Docker Desktop and confirms that the new container image, background-demo, has been created. The initial container size is 221 MB.

Tim reassures viewers that this size can be reduced later.

Running the Container

To test the containerized application, Tim starts the container directly from Docker Desktop. Since there are no running containers yet, he clicks Run and sets an optional setting:

  • He maps the container’s internal port (randomly assigned) to port 8080 on the host machine.

After launching, he accesses the application using a web browser.\ Initially, accessing the root URL gives a 404 error, but navigating to /messages successfully displays the expected data.

Tim highlights an important point:

  • The application runs in UTC time within the container, which may differ from the local machine’s time zone.

He also notes that since the application was published in release mode, Swagger UI is disabled by default.

Optimizing the Container Size

At this point, the default container size is 221 MB, which Tim acknowledges as quite large. To reduce the size, he introduces a lighter base image.

By modifying the Program.cs file, he sets the container family to jammy-chiseled, a smaller Ubuntu-based image:

builder.Services.Configure<ContainerOptions>(options => options.ContainerFamily = "jammy-chiseled");
builder.Services.Configure<ContainerOptions>(options => options.ContainerFamily = "jammy-chiseled");
TODO
$vbLabelText   $csharpLabel

He then reruns the publish command, and after rebuilding the container, he checks the image size again.

The new image size is 113 MB—a reduction of about 100 MB! Despite the smaller size, the application functions identically.

Deleting and Recreating Containers

Tim demonstrates deleting the container to clean up the workspace. However, the Docker image remains available, so the container can be recreated if needed.

He emphasizes that once the container is built, it can be:

  • Run locally for development and testing.

  • Pushed to a container registry like Docker Hub or Azure Container Registry.

  • Used as the base for a cloud-deployed web application.

Conclusion and Final Thoughts

Tim wraps up the video by reinforcing the key benefits of using the .NET CLI for containerization:

  1. No need for a Dockerfile – The .NET CLI handles everything automatically.

  2. Quick and easy process – With just one command, an application can be containerized.

  3. Smaller, optimized images – Using jammy-chiseled reduces container size.

  4. Seamless deployment – The container can be pushed to various cloud services.

Final Thoughts

Tim’s approach to containerizing C# applications with the .NET CLI is simple, fast, and effective. By following his method, developers can create and optimize containers without needing extensive Docker knowledge.

By leveraging Linux containers, optimized base images, and streamlined publishing commands, this technique allows developers to efficiently containerize their applications with minimal effort.

If you want to dive deeper, be sure to check out Tim Corey’s video. It provides a clear, step-by-step walkthrough of the entire process.

Hero Worlddot related to Containerize C# Application Easily with the .NET CLI
Hero Affiliate related to Containerize C# Application Easily with the .NET CLI

Earn More by Sharing What You Love

Do you create content for developers working with .NET, C#, Java, Python, or Node.js? Turn your expertise into extra income!