Simple .NET Containers: Create Containerized Apps with dotnet publish

Containers are easy to distribute and can run just about anywhere. At this point, you can download just about any popular software as a container…

PostgreSQL? Check!
mongoDB? Sure!
Redis? Absolutely!

So, it certainly makes sense if you are planning to release your next software project as a container based solution. And, if you use. NET for software development, developing container based solutions may have just got a bit easier for you. In .NET 7, Microsoft introduced publishing a .NET project as a Docker image by simply using the dotnet publish command.

Photo by frank mckenna on Unsplash

The Solution

In this tutorial, we’ll go over creating a .NET based Docker container using the .NET 7 SDK. In addition, we’ll create a running application and run the container to test it out.

Remember, for any example solution from AWS with .NET, we focus on the code that exemplifies the problem we are trying to solve. We don’t include logging, input validation, exception handling, etc., and we embed the configuration data within classes instead of using environment variables, configuration files, key/value stores and the like. These items should not be skipped for proper solutions.

Prerequisites

To complete this tutorial, you will need the .NET CLI which is included in the .NET 7 SDK. In addition, you will need to download and install Docker.

Warning: some AWS services may have fees associated with them.

Our Dev Environment

This tutorial was developed using Ubuntu 20.04, .NET 7 SDK and Visual Studio Code 1.73.1. Some commands/constructs may very across systems.

Publishing a .NET Docker Image

Create the .NET Application Project

Our first task is to create the web application with the following command:

$ dotnet new web -n container-app

This command will produce a .NET web application with the following logic in the Program.cs file.

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

app.MapGet(“/”, () => “Hello World!”);

app.Run();

Add Dependencies

In order to publish our new app to a Docker image, we need to add one dependency like so:

$ dotnet add package Microsoft.NET.Build.Containers

Publish the App

Let’s publish the .NET web application with the following command:

$ dotnet publish ––os linux ––arch x64 /t:PublishContainer -c Release

To verify that the image was created, run the following command:

$ docker image ls

You should see the container listed something like this:

Run the newly created Docker image with the following command:

$ docker run -it -p 5000:80 container-app:1.0.0

Test the Running Docker Container

Open a browser window and browse to, http://localhost:5000/ and you will see the following content that lines up with the endpoint created in Program.cs:

Hello World!

Summary

We have concluded this tutorial where you have learned how to publish a .NET app directly to a Docker image.

Challenges

Now that you have a working solution, let’s take things a little further…

Want to know more about the tech in this article?  Checkout these resources:

.NET CLI, .NET SDK, Docker