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…
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
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.
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!”);
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:
We have concluded this tutorial where you have learned how to publish a .NET app directly to a Docker image.
Now that you have a working solution, let’s take things a little further…
- Configure custom parameters of the Docker image, like: the base image, container registry, container image name, etc.
- After configuring your image, run the newly created image in AWS EKS or AWS ECS.
Want to know more about the tech in this article? Checkout these resources: