Docker Basics for .NET

Build once, run anywhere. Stop saying "works on my machine". 8 mins.

Your First "Aha" Moment: Docker image = Your app + .NET runtime + OS. One zip file that runs anywhere.

1. The Problem Docker Solves

Without Docker: "It works on my laptop." Prod server: missing .NET 8, wrong OS, different paths. Crash.
With Docker: Ship your app + .NET 8 + Ubuntu in one container. Server just needs Docker. Runs identical everywhere.

2. Dockerfile - 4 Lines That Matter

Create Dockerfile in project root:

# 1. Build stage: SDK has compiler
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
WORKDIR /src
COPY *.csproj .
RUN dotnet restore
COPY . .
RUN dotnet publish -c Release -o /app

# 2. Runtime stage: Only runtime, 100MB not 1GB
FROM mcr.microsoft.com/dotnet/aspnet:8.0
WORKDIR /app
COPY --from=build /app .
ENTRYPOINT ["dotnet", "MyApp.dll"]

Key: Multi-stage. Build with SDK. Run with ASPNET runtime. Final image ~200MB not 1.2GB.

3. Run It Locally - 3 Commands

docker build -t myapp .
docker run -d -p 8080:8080 --name myapp myapp
curl http://localhost:8080/weatherforecast

-p 8080:8080 = Map host port 8080 β†’ container port 8080. Set in launchSettings.json: "ASPNETCORE_URLS": "http://+:8080"

Career-Killer Mistake #1: Using SDK image for runtime.
FROM mcr.microsoft.com/dotnet/sdk:8.0 in final stage.
What happens: Image is 1.2GB vs 200MB. Pull takes 5 min. 10 pods = 12GB RAM wasted. AWS bill +$300/month.
Fix: Always multi-stage. Final stage = aspnet:8.0 or runtime:8.0.

4. docker-compose - Run App + DB Together

docker-compose.yml

services:
  web:
    build: .
    ports: ["8080:8080"]
    environment:
      - ConnectionStrings__Default=Server=db;Database=App;User=sa;Password=YourStrong@Passw0rd
    depends_on: [db]
  db:
    image: mcr.microsoft.com/mssql/server:2022-latest
    environment:
      - ACCEPT_EULA=Y
      - MSSQL_SA_PASSWORD=YourStrong@Passw0rd
    ports: ["1433:1433"]

Run: docker-compose up -d. App + SQL Server up in 20s. Destroy: docker-compose down -v.

Stop Here. Think. Container is ephemeral. Logs, uploads, SQLite DB gone on restart.
Mount volumes for data: -v mydata:/app/data. For logs: use stdout + ship to Seq/ELK.

Quick Check 🧠

⚠️ This is not the end

This is V1 Draft Version

We are upgrading. Don't judge book by its cover.

Picture abhi baki hai mere dost 🎬


Deployment section complete. Content is V1 for internal review.
Real devs: deploy this, break it, tell us what’s missing. We're shipping V2 based on your feedback.

Deployment Track Complete! IIS β†’ Azure β†’ Docker. You can now ship code.
What’s next? Tell me: Advanced C#, Microservices, K8s, or Testing. I'll build the next section. You choose.

Comments on Docker Basics (0)

No comments yet. Be the first to share your thoughts!