Publishing and Docker

Back

Loading concept...

πŸš€ 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:

  1. Pack everything including the LEGO bricks, instructions, and even the floor mat
  2. 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

  1. Publishing = Packing your app for travel
  2. Self-Contained = Everything included (bigger but independent)
  3. Framework-Dependent = Just your code (smaller but needs .NET)
  4. Docker = Magic container that works everywhere
  5. Dockerfile = Recipe to build your container
  6. 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!

Loading story...

Story - Premium Content

Please sign in to view this story and start learning.

Upgrade to Premium to unlock full access to all stories.

Stay Tuned!

Story is coming soon.

Story Preview

Story - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.