🐳 Week 4: Containers & Docker

Day 1: Introduction to Containerization

⏱ Duration: 5 Hours

πŸ“š Learning Objectives

  • Understand what containerization is and why it matters
  • Compare containers vs virtual machines
  • Install Docker on your system
  • Run your first container
  • Understand Docker architecture

πŸ“– Core Concepts (2 Hours)

What is Containerization?

Containers package an application with all its dependencies into a standardized unit. "It works on my machine" becomes "It works everywhere."

  • Isolation: Each container runs in its own environment
  • Portability: Run the same container anywhere
  • Lightweight: Share host OS kernel, no full OS needed
  • Consistency: Same behavior in dev, test, and production

Containers vs Virtual Machines

Virtual Machines: Containers: β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ App A β”‚ App B β”‚ β”‚ App A β”‚ App B β”‚ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€ β”‚ Guest β”‚ Guest β”‚ β”‚ Runtime β”‚Runtimeβ”‚ β”‚ OS β”‚ OS β”‚ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€ β”‚ Container Engineβ”‚ β”‚ Hypervisor β”‚ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ β”‚ Host OS β”‚ β”‚ Host OS β”‚ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ β”‚ Hardware β”‚ β”‚ Hardware β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ VMs: Heavy (GBs), Slow startup, Full OS per VM Containers: Light (MBs), Fast startup, Share kernel

Installing Docker

# Ubuntu/Debian sudo apt update sudo apt install -y docker.io # Add user to docker group (avoid sudo) sudo usermod -aG docker $USER newgrp docker # Verify installation docker --version docker run hello-world # Start Docker service sudo systemctl start docker sudo systemctl enable docker

Docker Architecture

Docker Components: - Docker Client: CLI commands (docker run, build, etc.) - Docker Daemon: Background service managing containers - Docker Registry: Storage for images (Docker Hub) - Docker Image: Template for containers (read-only) - Docker Container: Running instance of an image

Basic Docker Commands

# Run a container docker run hello-world docker run -it ubuntu bash # Interactive terminal docker run -d nginx # Detached (background) docker run -p 8080:80 nginx # Port mapping # List containers docker ps # Running containers docker ps -a # All containers # Stop/Remove containers docker stop container_id docker rm container_id docker rm -f container_id # Force remove running # Pull images docker pull nginx docker pull python:3.9 # List images docker images

πŸ”¬ Hands-on Lab (2.5 Hours)

Lab 1: First Containers

  • Run hello-world container
  • Run interactive Ubuntu container
  • Explore container file system
# Lab 1: Getting started docker run hello-world # Interactive Ubuntu docker run -it ubuntu bash # Inside container: cat /etc/os-release ls / exit # Run nginx web server docker run -d -p 8080:80 --name my-nginx nginx curl http://localhost:8080 docker stop my-nginx docker rm my-nginx

Lab 2: Container Lifecycle

  • Create, start, stop, restart containers
  • View container logs
  • Execute commands in running containers
# Lab 2: Container management docker run -d --name web nginx docker ps docker logs web docker exec -it web bash # Inside: nginx -v && exit docker stop web docker start web docker restart web docker rm -f web

βœ… Day 1 Checklist

  • Understand containers vs VMs
  • Docker installed and working
  • Can run containers (docker run)
  • Can manage containers (ps, stop, rm)
  • Understand Docker architecture