I’ve been building .NET Core Web API micro-services for a while now and have been loving it. However, when it came to Docker image size, it did not hold a candle to a Golang API using a SCRATCH base image. Some of my production images that are written in .NET Core 2.0 were typically coming in around 200–300MB. While Go is fast and compiles down amazingly well, I find that the language and tooling leaves a bit to be desired and sometimes I still long for Visual Studio and all its syntactic sugar. With the release of .NET Core 3.1 LTS last year, I decided it was finally time to revisit using it to build a tiny WebAPI container.

There were a few requirements on this journey. These are goals I set for myself in order to push the limits of .NET Core during this experiment.

  • Linux Environment Only: These micro-services will be deployed in Docker containers on Kubernetes, so a Linux environment was a must.
  • Tiny Container Footprint: Getting .NET Core down to under 10MB was most likely not in the cards, but the target was 50MB.
  • No Multi-Stage Dockerfile: Most examples online of .NET Core APIs in containers use a multi-stage Dockerfile. I did not want this requirement as part of the project. Multi-stage Docker builds are great, but introduce complexity and time. Plus, with .NET Core 3.0, this just isn’t needed anymore.
  • MacOS Development Environment: My development environment is macOS, so this is a hard requirement for me.

Scaffolding the WebAPI

Let’s get started with building out a base .NET Core WebAPI in Visual Studio. We’ll use the “New Project” wizard and select “API” from the “.NET Core App” left hand nav.

#dotnet-core #docker #docker container #.net core 3.1 #linux

Single-File Executables in .NET Core 3.1 and the Quest for a Sub-50MB Docker Container
1.50 GEEK