Building a Multiarch Microservice on .NET 5 and C# 9

Roberto Javier Yudice Monico
3 min readNov 27, 2020
Photo by Liam Briese on Unsplash

ARM CPU’s are no longer a mobile thing only. AWS has ARM-based instances which offer the same processing power and x86 based instance in most types of workloads often at a lower cost. Microsoft is roumored to release ARM instances soon, and Apple just released their M1 macbooks which use their ARM CPU that beats more expensive Intel-based Macbook on most workloads. At the current pace, ARM CPU’s are not only catching up but might soon overpass x86 CPU’s.

As ARM CPU continue to improve, running ARM and X86 applications in container workloads will become more common soon. Fortunately docker is already ready for this.

.NET 5 Multiarch images

Fortunately for us, Microsoft started publishing multiarch tags of .NET a while ago, so we already have a set of multiarch .NET 5 base images that we can work with.

The Microservice

We are going to build a simple microservice so that if you’d like you can deploy the multiarch images to your cluster and see how it works. We are going to use a very small microservice that only consists of two files, including the .csproj file.

The above will only compile on .NET 5, I’m using the new top-level statements feature of C# 9, which saves us some boilerplate code and makes it look cleaner and simpler.

Build the Docker image

For our microservice we are going to use a simple multistage build image. You will note that the Dockerfile doesnt change much — There is no reference to the multiarchitecture builld, this is because microsoft already publishes multiarchitecture images so Docker will automatically look for them based on the platforms that we pass in the arguments to the docker CLI, as you will see later.

Just to clarify a bit more on how Docker finds the base image that corresponds to the platform, if you go into the docker hub page for dotnet you will see that the arch of the image is always a string at the end of the image:

Using Docker buildx

Docker currently already supports docker images for multiple architectures, although it’s still an experimental feature that you have to enable in the settings.

Now that we have the Dockerfile we can build our multiplatform image. As mentioned before, this is an experimental feature, you can read more about it here, but first we will have to enable the experimental feature in the docker settings window:

After enabling them now you can run the following command to build you docker image:

docker buildx build --tag ryudice/net5-multiarch:latest --push --platform linux/arm64/v8,linux/amd64 .

I’m using my Dockerhub account there so if you want to publish the image replace it with the URL of your registry.

After the command finishes the image will show up in your registry and it should have two platforms: arm64 and amd64.

And there you go, now you can pull your image in your EKS Graviton instances or any other ARM platform.

You can find all the code for this article here.

--

--