Module 5: Docker Introduction

Containerization with Docker for application deployment.

Duration: 60 minutes

Learning Objectives

  • • Understand Docker concepts and benefits
  • • Create and manage Docker containers
  • • Build Docker images using Dockerfiles
  • • Use Docker Compose for multi-container applications

Key Topics

Docker Concepts and Benefits

Understanding containerization and its advantages over traditional virtualization.

# Docker benefits:
- Consistent environments across development, testing, and production
- Lightweight compared to virtual machines
- Easy scaling and deployment
- Isolation of applications and dependencies
- Version control for application environments

Creating Dockerfiles

Writing Dockerfiles to define application environments and dependencies.

# Dockerfile for ASP.NET Core application
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
WORKDIR /src
COPY ["StudentApp/StudentApp.csproj", "StudentApp/"]
RUN dotnet restore "StudentApp/StudentApp.csproj"
COPY . .
WORKDIR "/src/StudentApp"
RUN dotnet build "StudentApp.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "StudentApp.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "StudentApp.dll"]

Container Management

Managing Docker containers, images, and networks.

# Basic Docker commands
docker build -t student-app .
docker run -d -p 8080:80 --name student-container student-app
docker ps -a
docker logs student-container
docker stop student-container
docker rm student-container
docker images
docker rmi student-app

Docker Compose for Multi-Container Apps

Orchestrating multiple containers with Docker Compose.

# docker-compose.yml
version: '3.8'
services:
  web:
    build: .
    ports:
      - "8080:80"
    depends_on:
      - db
    environment:
      - ConnectionStrings__DefaultConnection=Server=db;Database=StudentDB;User=sa;Password=YourPassword123;
  
  db:
    image: mcr.microsoft.com/mssql/server:2022-latest
    environment:
      - ACCEPT_EULA=Y
      - SA_PASSWORD=YourPassword123
    ports:
      - "1433:1433"

Hands-on Exercises

Exercise 1: Containerize Student App

Create a Dockerfile for the ASP.NET Core application from Module 3.

Exercise 2: Multi-Container Setup

Use Docker Compose to run the application with a SQL Server database.

Exercise 3: Container Optimization

Optimize the Docker image size and build time using multi-stage builds.