#csharp #dotnet #backend

Day 30: Production, Docker, & Deployment

Welcome to Day 30! You have mastered the fundamentals of C#, Minimal APIs, EF Core, Authentication, and Testing. It’s time to unleash your API onto the internet.

While you could deploy an ASP.NET API directly to Azure App Service or an IIS server, modern cloud deployments run on Docker.

Why Docker?

“It works on my machine!”

Docker solves this permanently. Docker packages your C# API, the .NET Runtime, and whatever Linux OS files it needs into a single, standardized box called a Container. If the container works on your laptop, that exact same container will work flawlessly on AWS, Google Cloud, DigitalOcean, or Azure.

The Dockerfile

To containerize a .NET application, you create a text file named Dockerfile at the root of your project. This file gives Docker the exact instructions on how to build and package your app.

Here is the standard multi-stage build template for .NET APIs:

# 1. Start with the .NET SDK image to BUILD the app (it includes compilers)
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
WORKDIR /src

# 2. Copy the csproj file and restore packages (Optimized CACHING!)
COPY ["MyApi.csproj", "./"]
RUN dotnet restore "MyApi.csproj"

# 3. Copy everything else and build the release binary
COPY . .
RUN dotnet publish "MyApi.csproj" -c Release -o /app/publish

# 4. SWAP to the incredibly lightweight ASP.NET Runtime image for PRODUCTION
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS final
WORKDIR /app

# 5. Copy ONLY the compiled binaries from the build stage into the final image
COPY --from=build /app/publish .

# 6. Expose the standard port and run it!
EXPOSE 80
ENTRYPOINT ["dotnet", "MyApi.dll"]

Because we use a massive heavy SDK image to build the code (Step 1), and a microscopic fast Runtime image to serve the code (Step 4), our final output is incredibly small, secure, and fast!

Building and Running Locally

To transform the Dockerfile into a package, run:

docker build -t my-csharp-api .

To run that package on your machine at port 8080:

docker run -p 8080:80 my-csharp-api

You can now hit localhost:8080 and your API will respond, completely encapsulated in its container!

Deployment Options

Once your app is containerized, you can deploy it literally anywhere.

  1. Azure Container Apps (The easiest managed serverless environment for .NET)
  2. AWS Fargate or Google Cloud Run
  3. DigitalOcean Apps (Connects directly to your GitHub repo and builds the Dockerfile for you).

The End of the Beginning

Congratulations! You survived the 30-Day C# and ASP.NET Core masterclass.

You’ve moved from writing Console.WriteLine() to designing RESTful, Database-backed, JWT-secured, Containerized Microservices.

The next step is to start building real-world projects. Build a blog backend, a Discord bot, or a budget tracker. The .NET ecosystem is massive—enjoy the ride!