Containerize Your .NET Web App with Docker

You’ve been building .NET applications for years—maybe decades—and you’re wondering if Docker containers are worth the hype. Spoiler: they are. Containers let you package your app, its dependencies, and even its runtime into a single, portable unit. No more “it works on my machine” excuses. At Funcular Labs, we’ve seen firsthand how Docker streamlines deployment, testing, and scaling for .NET developers. Let’s walk through setting up Docker and deploying a simple “Hello World” ASP.NET app to both Windows and Linux containers. It’s easier than you might think.
Why Docker for .NET?
Containers isolate your app, ensuring consistency across development, testing, and production. They’re lightweight compared to VMs, and you can run them anywhere—your laptop, a server, or the cloud. For .NET developers, Docker supports both Windows and Linux containers, giving you flexibility to target different environments. Plus, it’s a great way to modernize legacy ASP.NET apps or build new ones with .NET Core or .NET 8.
Prerequisites: Setting Up Your Environment
Before we dive in, let’s get your machine ready:
- Docker Desktop: Download and install from Docker’s website. For Windows, ensure Hyper-V is enabled (Windows Pro/Enterprise) or WSL 2 is set up for Linux containers.
- .NET SDK: Version 8.0 or later for cross-platform development. Grab it from Microsoft’s site.
- Visual Studio or VS Code: Either works, but Visual Studio 2022 has great Docker tooling.
- Windows Containers Setup (for Windows containers): Enable the “Containers” and “Hyper-V” Windows features via
Turn Windows features on or off
.
Once Docker Desktop is installed, switch between Windows and Linux container modes via the system tray icon. We’ll use both in this post.
Creating a Simple ASP.NET “Hello World” App
Let’s create a minimal ASP.NET app that outputs “Hello World.” Fire up a terminal and run:
dotnet new web -n HelloWorldApp
cd HelloWorldApp
This creates a basic ASP.NET Core app. Modify Program.cs
to keep it dead simple:
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.MapGet("/", () => "Hello World");
app.Run();
Test it locally with dotnet run
. Navigate to http://localhost:5000
, and you should see “Hello World.” Now, let’s containerize it.
Deploying to a Windows Container
Windows containers are perfect for legacy ASP.NET apps or when you need IIS. First, create a Dockerfile
in the project root:
FROM mcr.microsoft.com/dotnet/aspnet:8.0-nanoserver-ltsc2022
WORKDIR /app
COPY ./bin/Release/net8.0/publish/ .
ENTRYPOINT ["dotnet", "HelloWorldApp.dll"]
Publish the app for release:
dotnet publish -c Release
Ensure Docker Desktop is in Windows container mode. Build and run the container:
docker build -t helloworld-windows .
docker run -d -p 8080:80 helloworld-windows
Visit http://localhost:8080
. You’ll see “Hello World” served from a Windows container. The nanoserver-ltsc2022
base image keeps things lightweight but includes the .NET runtime.
Deploying to a Linux Container
Linux containers are leaner and more common in cloud environments. Create a new Dockerfile
(or overwrite the previous one):
FROM mcr.microsoft.com/dotnet/aspnet:8.0
WORKDIR /app
COPY ./bin/Release/net8.0/publish/ .
ENTRYPOINT ["dotnet", "HelloWorldApp.dll"]
Switch Docker Desktop to Linux container mode. Build and run:
docker build -t helloworld-linux .
docker run -d -p 8081:80 helloworld-linux
Head to http://localhost:8081
, and there’s your “Hello World” again, now running on Linux. The aspnet:8.0
image is optimized for Linux and is smaller than its Windows counterpart.
Next Steps
You’ve just deployed a .NET app to both Windows and Linux containers—congratulations! From here, explore multi-stage Dockerfiles to streamline builds, or try orchestrating containers with Docker Compose or Kubernetes. At Funcular Labs, we’ve helped clients cut deployment times by 40% using Docker. It’s a game-changer.
Got questions? Hit us up in the comments or reach out to Funcular Labs. Happy containerizing!