Docker & CI/CD Pipelines


What is Docker?

Docker = Platform for developing, shipping, and running applications in containers

Container = Lightweight, standalone, executable package that includes everything needed to run an application

Key Concepts:

  • Isolation - Each container runs independently

  • Portability - Run anywhere (dev, test, prod)

  • Consistency - Same environment everywhere

  • Efficiency - Lightweight compared to VMs

  • Scalability - Easy to scale horizontally

Docker vs Virtual Machines

Virtual Machine:
┌─────────────────────────────┐
│     Application             │
├─────────────────────────────┤
│     Guest OS (GB)           │
├─────────────────────────────┤
│     Hypervisor              │
├─────────────────────────────┤
│     Host OS                 │
└─────────────────────────────┘

Docker Container:
┌─────────────────────────────┐
│     Application             │
├─────────────────────────────┤
│     Docker Engine           │
├─────────────────────────────┤
│     Host OS                 │
└─────────────────────────────┘

Advantages of Docker:

  • ⚡ Faster startup (seconds vs minutes)

  • 💾 Less disk space (MB vs GB)

  • 🚀 Better resource utilization

  • 📦 Easier to distribute


Docker Installation

Windows

Linux


Docker Core Concepts

Images

Image = Read-only template with instructions for creating a container

Containers

Container = Runnable instance of an image


Dockerfile for .NET Applications

Basic Dockerfile

Multi-Stage Dockerfile Explained

Why Multi-Stage?

  • ✅ Smaller final image (no SDK, only runtime)

  • ✅ Build tools not in production image

  • ✅ Better security

  • ✅ Faster deployment

Dockerfile with Environment Variables


Building and Running Docker Images

Build Image

Run Container

Container Management


Docker Compose

Docker Compose = Tool for defining and running multi-container applications

Basic docker-compose.yml

Complete docker-compose.yml with Multiple Services

Docker Compose Commands


.dockerignore File


Docker Best Practices

1. Use Multi-Stage Builds

2. Leverage Build Cache

3. Use Specific Image Tags

4. Run as Non-Root User

5. Minimize Layers


CI/CD Fundamentals

CI/CD = Continuous Integration / Continuous Deployment

Continuous Integration (CI)

Process:

  1. Developer commits code

  2. CI server detects change

  3. Code is built

  4. Tests are run

  5. Feedback is provided

Benefits:

  • ✅ Early bug detection

  • ✅ Automated testing

  • ✅ Consistent builds

  • ✅ Faster integration

Continuous Deployment (CD)

Process:

  1. Code passes CI

  2. Automated deployment to staging

  3. Run integration tests

  4. Deploy to production (automatically or with approval)

Benefits:

  • ✅ Faster releases

  • ✅ Reduced manual errors

  • ✅ Consistent deployments

  • ✅ Quick rollbacks


GitHub Actions

Basic Workflow

Build, Test, and Docker

Deploy to Azure

Matrix Strategy (Multiple Versions)


Azure DevOps Pipelines

Basic Pipeline

Docker Build and Push

Multi-Stage Pipeline (Build, Test, Deploy)


Deployment Strategies

1. Blue-Green Deployment

Advantages:

  • ✅ Zero downtime

  • ✅ Easy rollback

  • ✅ Test in production-like environment

Docker Compose Example:

2. Rolling Deployment

Advantages:

  • ✅ No additional infrastructure needed

  • ✅ Gradual rollout

  • ✅ Can stop if issues detected

Kubernetes Example:

3. Canary Deployment

Advantages:

  • ✅ Test with real users

  • ✅ Limited blast radius

  • ✅ Gradual rollout


Health Checks

ASP.NET Core Health Checks

Docker Health Check

Docker Compose with Health Check


Secrets Management

GitHub Secrets

Azure Key Vault

Docker Secrets


Monitoring and Logging

Application Insights in Docker

Logging with Serilog

Docker Volume for Logs


Best Practices Summary

Docker

  1. ✅ Use multi-stage builds

  2. ✅ Leverage build cache

  3. ✅ Use specific image tags

  4. ✅ Run as non-root user

  5. ✅ Keep images small

  6. ✅ Use .dockerignore

  7. ✅ Add health checks

  8. ✅ Set resource limits

CI/CD

  1. ✅ Run tests before deploy

  2. ✅ Use environment-specific configs

  3. ✅ Implement health checks

  4. ✅ Use secrets management

  5. ✅ Enable rollback capability

  6. ✅ Monitor deployments

  7. ✅ Use staging environment

  8. ✅ Automate everything


Quick Reference: Docker Commands


Guide Complete! This comprehensive Docker & CI/CD guide covers containerization, multi-stage builds, Docker Compose, GitHub Actions, Azure DevOps Pipelines, deployment strategies, and best practices for deploying .NET applications! 🐳

Last updated