π ASP.NET Deployment: Publishing & Docker
The Moving Day Metaphor π¦
Imagine youβve built an amazing LEGO castle in your room. Now your friend across town wants to see it! You have two choices:
- Pack everything including the LEGO bricks, instructions, and even the floor mat
- Just pack the castle and assume your friend has their own LEGO bricks
This is exactly what ASP.NET deployment is about! Letβs explore how to share your app with the world.
π― Publishing Options
Publishing is like packing your app into a suitcase for its journey to a server.
What is Publishing?
When you write code, itβs in a language humans understand. Publishing transforms it into something computers can run directly.
# The magic command to publish
dotnet publish -c Release
Think of it like baking:
- Your code = recipe ingredients
- Publishing = baking process
- Published app = ready-to-eat cake π
π¦ Self-Contained Deployment (SCD)
The Complete Package
Self-contained means you pack EVERYTHINGβeven the oven to bake the cake!
dotnet publish -c Release \
--self-contained true \
-r win-x64
Whatβs Inside?
π Published Folder
βββ π― YourApp.exe
βββ π YourApp.dll
βββ βοΈ .NET Runtime files
βββ π¦ All dependencies
βββ π§ Native libraries
Pros & Cons
| β Pros | β Cons |
|---|---|
| Works on ANY machine | Larger file size |
| No .NET install needed | Must publish per OS |
| Version independence | More to download |
Example: Publishing for different systems:
# For Windows
dotnet publish -r win-x64 \
--self-contained
# For Linux
dotnet publish -r linux-x64 \
--self-contained
# For Mac
dotnet publish -r osx-x64 \
--self-contained
πͺΆ Framework-Dependent Deployment (FDD)
The Light Traveler
Framework-dependent is like sending just your castleβyour friend must have LEGO bricks already!
dotnet publish -c Release \
--self-contained false
Whatβs Inside?
π Published Folder
βββ π― YourApp.dll
βββ π YourApp.deps.json
βββ βοΈ YourApp.runtimeconfig.json
βββ π¦ Your dependencies only
Comparison Chart
graph TD A["Your App"] --> B{Deployment Type?} B --> C["Self-Contained"] B --> D["Framework-Dependent"] C --> E["Big Package<br/>~70MB+"] D --> F["Small Package<br/>~5MB"] E --> G["β Runs Anywhere"] F --> H["β οΈ Needs .NET Installed"]
When to Use What?
| Scenario | Best Choice |
|---|---|
| Client doesnβt have .NET | Self-Contained |
| Server already has .NET | Framework-Dependent |
| Need smallest size | Framework-Dependent |
| Need maximum control | Self-Contained |
π³ Docker Basics
What is Docker?
Docker is like a magic shipping container for your app. No matter what computer it goes to, it works the same way!
The Pizza Box Analogy:
- Without Docker = Giving someone pizza ingredients
- With Docker = Giving them a complete pizza in a box π
Key Concepts
graph TD A["Dockerfile"] -->|build| B["Image"] B -->|run| C["Container"] C --> D["Your Running App!"]
| Term | What It Is | Real-World Example |
|---|---|---|
| Image | Blueprint | Recipe card |
| Container | Running instance | Actual cooked meal |
| Dockerfile | Instructions | Cooking steps |
Basic Commands
# Build an image
docker build -t myapp .
# Run a container
docker run -p 8080:80 myapp
# See running containers
docker ps
# Stop a container
docker stop container_id
π Dockerfile for ASP.NET
The Recipe for Your App
A Dockerfile tells Docker exactly how to package your app.
Simple Dockerfile
# Start with Microsoft's image
FROM mcr.microsoft.com/dotnet/aspnet:8.0
WORKDIR /app
# Copy published files
COPY ./publish .
# Tell Docker the door number
EXPOSE 80
# Start the app
ENTRYPOINT ["dotnet", "MyApp.dll"]
Multi-Stage Dockerfile (Pro Level!)
This is like having a messy kitchen for cooking and a clean plate for serving:
# STAGE 1: Build (messy kitchen)
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
# STAGE 2: Run (clean plate)
FROM mcr.microsoft.com/dotnet/aspnet:8.0
WORKDIR /app
COPY --from=build /app .
EXPOSE 80
ENTRYPOINT ["dotnet", "MyApp.dll"]
Why Multi-Stage?
graph LR A["SDK Image<br/>800MB"] -->|build| B["Your Code"] B -->|publish| C["Runtime Image<br/>200MB"] C --> D["Final Image<br/>~250MB"]
Result: Your final image is 4x smaller! π
πΌ Docker Compose
The Orchestra Conductor
What if your app needs a database AND a cache? Docker Compose manages multiple containers together!
Think of it like:
- Single container = One musician
- Docker Compose = Full orchestra π»
docker-compose.yml Example
version: '3.8'
services:
# Your ASP.NET app
web:
build: .
ports:
- "8080:80"
depends_on:
- db
environment:
- ConnectionStrings__Default=Server=db;Database=mydb;User=sa;Password=Pass123!
# SQL Server database
db:
image: mcr.microsoft.com/mssql/server:2022-latest
environment:
- ACCEPT_EULA=Y
- SA_PASSWORD=Pass123!
volumes:
- sqldata:/var/opt/mssql
# Persistent storage
volumes:
sqldata:
Compose Commands
# Start everything
docker-compose up
# Start in background
docker-compose up -d
# Stop everything
docker-compose down
# See logs
docker-compose logs -f web
How Services Connect
graph TD A["User Browser"] -->|port 8080| B["Web Container"] B -->|port 1433| C["Database Container"] C --> D["#40;sqldata volume#41;"] style B fill:#4ECDC4 style C fill:#FF6B6B
Complete Compose Example
version: '3.8'
services:
api:
build:
context: .
dockerfile: Dockerfile
ports:
- "5000:80"
environment:
- ASPNETCORE_ENVIRONMENT=Production
depends_on:
- redis
- postgres
redis:
image: redis:alpine
ports:
- "6379:6379"
postgres:
image: postgres:15
environment:
POSTGRES_USER: admin
POSTGRES_PASSWORD: secret
POSTGRES_DB: appdb
volumes:
- pgdata:/var/lib/postgresql/data
volumes:
pgdata:
π― Quick Reference
Publishing Cheatsheet
| Command | What It Does |
|---|---|
dotnet publish |
Basic publish |
-c Release |
Release mode (faster) |
--self-contained |
Include .NET runtime |
-r win-x64 |
Target Windows 64-bit |
-o ./output |
Output folder |
Docker Cheatsheet
| Command | What It Does |
|---|---|
docker build -t name . |
Build image |
docker run -p 8080:80 name |
Run container |
docker-compose up |
Start all services |
docker-compose down |
Stop all services |
π Your Journey Map
graph TD A["Write Code"] --> B["Publish"] B --> C{Self-Contained?} C -->|Yes| D["Big but Independent"] C -->|No| E["Small but Needs .NET"] D --> F["Create Dockerfile"] E --> F F --> G["Build Image"] G --> H{Multiple Services?} H -->|Yes| I["Docker Compose"] H -->|No| J["Docker Run"] I --> K["π App is Live!"] J --> K
π‘ Key Takeaways
- Publishing = Packing your app for travel
- Self-Contained = Everything included (bigger but independent)
- Framework-Dependent = Just your code (smaller but needs .NET)
- Docker = Magic container that works everywhere
- Dockerfile = Recipe to build your container
- Docker Compose = Orchestra conductor for multiple containers
You did it! π Now you know how to take your ASP.NET app from your computer to the world!
