Docker Introduction

Part 1: Containers vs Virtual Machines

Virtual Machines (VMs)

What is a VM?

Components:

VM Architecture:

┌─────────────────────────────────────────────┐
│         Applications & Binaries             │
├─────────────────────────────────────────────┤
│            Guest OS (Full OS)               │
├─────────────────────────────────────────────┤
│              Hypervisor                     │
├─────────────────────────────────────────────┤
│              Host OS                        │
├─────────────────────────────────────────────┤
│         Physical Hardware (Server)          │
└─────────────────────────────────────────────┘

Pros:

Cons:


Docker Containers

What is a Container?

Container Architecture:

┌─────────────────────────────────────────────┐
│      Container 1    │    Container 2        │
│  ┌──────────────┐   │  ┌──────────────┐    │
│  │ App + Libs   │   │  │ App + Libs   │    │
│  └──────────────┘   │  └──────────────┘    │
├─────────────────────────────────────────────┤
│          Docker Engine (Daemon)             │
├─────────────────────────────────────────────┤
│              Host OS                        │
├─────────────────────────────────────────────┤
│         Physical Hardware (Server)          │
└─────────────────────────────────────────────┘

Pros:

Cons:


Key Differences: VMs vs Containers

Feature Virtual Machines Containers
Size Gigabytes Megabytes
Startup Time Minutes Seconds
OS Each VM has full OS Share host OS kernel
Isolation Complete (hardware-level) Process-level
Resource Usage Heavy Lightweight
Portability Less portable Highly portable
Use Case Running different OS types Running multiple apps on same OS

Part 2: Docker Architecture

Docker Components

1. Docker Daemon (dockerd)

2. Docker Client (docker)

3. Docker Registry (Docker Hub)

4. Docker Objects

Docker Architecture Diagram

┌──────────────────────────────────────────────────────┐
│                   Docker Client                       │
│              (CLI: docker commands)                   │
└────────────────────┬─────────────────────────────────┘
                     │ REST API
                     ▼
┌──────────────────────────────────────────────────────┐
│                  Docker Daemon                        │
│                    (dockerd)                          │
│  ┌──────────────────────────────────────────────┐   │
│  │  Container Management                        │   │
│  │  Image Management                            │   │
│  │  Network Management                          │   │
│  │  Volume Management                           │   │
│  └──────────────────────────────────────────────┘   │
└────────────────────┬─────────────────────────────────┘
                     │
                     ▼
┌──────────────────────────────────────────────────────┐
│              Docker Registry (Docker Hub)             │
│          (Stores and distributes images)              │
└──────────────────────────────────────────────────────┘

How Docker Works - The Flow

  1. Developer creates Dockerfile → Defines application and dependencies
  2. Build imagedocker build creates image from Dockerfile
  3. Push to registrydocker push uploads image to Docker Hub
  4. Pull from registrydocker pull downloads image from Docker Hub
  5. Run containerdocker run creates container from image
  6. Container runs → Isolated application instance executes

Part 3: Essential Docker Commands

Working with Docker Hub

Docker Hub (hub.docker.com)

Image Naming Convention:

repository/image:tag

Examples:

Searching Images on Docker Hub

Via Website:

  1. Go to hub.docker.com
  2. Use search bar to find images
  3. Check image details, tags, and documentation
  4. Look for official images (verified badge)

Via Command Line:

docker search nginx
docker search mysql
docker search ubuntu

Core Docker Commands Covered in Class

1. docker pull

Purpose: Downloads an image from Docker Hub to your local machine

Syntax:

docker pull [IMAGE_NAME]:[TAG]

Examples:

# Pull latest version of nginx
docker pull nginx

# Pull specific version
docker pull nginx:1.21

# Pull Ubuntu 20.04
docker pull ubuntu:20.04

# Pull MySQL 8
docker pull mysql:8.0

What happens:


2. docker run

Purpose: Creates and starts a new container from an image

Syntax:

docker run [OPTIONS] [IMAGE_NAME]:[TAG]

Basic Examples:

# Run nginx (downloads image if not available locally)
docker run nginx

# Run in detached mode (background)
docker run -d nginx

# Run with custom name
docker run --name my-nginx nginx

# Run with port mapping (host:container)
docker run -p 8080:80 nginx

# Run interactively with terminal
docker run -it ubuntu bash

Common Options:

What happens:

  1. Checks if image exists locally
  2. If not, pulls from Docker Hub
  3. Creates a new container from image
  4. Starts the container
  5. Returns container ID

3. docker ps

Purpose: Lists containers

Syntax:

docker ps [OPTIONS]

Examples:

# Show running containers
docker ps

# Show all containers (running and stopped)
docker ps -a

# Show only container IDs
docker ps -q

# Show last created container
docker ps -l

# Show container sizes
docker ps -s

Output Information:


4. docker start

Purpose: Starts one or more stopped containers

Syntax:

docker start [CONTAINER_ID or CONTAINER_NAME]

Examples:

# Start container by name
docker start my-nginx

# Start container by ID
docker start a1b2c3d4

# Start multiple containers
docker start container1 container2

# Start and attach to container
docker start -a my-nginx

Key Points:


5. docker stop

Purpose: Stops one or more running containers gracefully

Syntax:

docker stop [CONTAINER_ID or CONTAINER_NAME]

Examples:

# Stop container by name
docker stop my-nginx

# Stop container by ID
docker stop a1b2c3d4

# Stop multiple containers
docker stop container1 container2

# Stop all running containers
docker stop $(docker ps -q)

What happens:

  1. Sends SIGTERM signal (graceful shutdown)
  2. Waits 10 seconds for container to stop
  3. If still running, sends SIGKILL (force stop)

Key Points:


Part 4: Docker Workflow - Putting It All Together

Typical Development Workflow

1. Find an Image

# Search on Docker Hub website or CLI
docker search nginx

2. Pull the Image

# Download image to local machine
docker pull nginx:latest

3. Run a Container

# Create and start container
docker run -d -p 8080:80 --name my-web-server nginx

4. Check Running Containers

# Verify container is running
docker ps

5. Stop Container

# Stop when done
docker stop my-web-server

6. Start Container Again

# Restart the same container
docker start my-web-server

7. Remove Container (cleanup)

# First stop if running
docker stop my-web-server

# Then remove
docker rm my-web-server

Part 5: Practice Exercises

Exercise 1: Run a Web Server

# Pull nginx image
docker pull nginx

# Run nginx on port 8080
docker run -d -p 8080:80 --name webserver nginx

# Check if running
docker ps

# Test in browser: http://localhost:8080

# Stop and remove
docker stop webserver
docker rm webserver

Exercise 2: Interactive Ubuntu Container

# Run Ubuntu interactively
docker run -it --name myubuntu ubuntu bash

# Inside container:
ls
pwd
whoami
exit

# Start again
docker start -i myubuntu

# Remove
docker rm myubuntu

Exercise 3: Working with Different Versions

# Pull multiple nginx versions
docker pull nginx:1.21
docker pull nginx:latest

# List images
docker images

# Run specific version
docker run -d --name nginx-old nginx:1.21
docker run -d --name nginx-new nginx:latest

# Check both running
docker ps

Part 6: Important Concepts to Remember

Container Lifecycle States

  1. Created: Container created but not started
  2. Running: Container is active and running
  3. Paused: Container processes are paused
  4. Stopped: Container has stopped
  5. Removed: Container deleted from system

Docker Daemon vs Docker Client

Images vs Containers


Quick Reference Card

Most Used Commands from This Lesson

# Search for images
docker search [IMAGE_NAME]

# Pull an image
docker pull [IMAGE_NAME]:[TAG]

# Run a container
docker run -d -p [HOST_PORT]:[CONTAINER_PORT] --name [NAME] [IMAGE]

# List running containers
docker ps

# List all containers
docker ps -a

# Stop a container
docker stop [CONTAINER_NAME/ID]

# Start a stopped container
docker start [CONTAINER_NAME/ID]

# Remove a container
docker rm [CONTAINER_NAME/ID]

# List images
docker images

# Remove an image
docker rmi [IMAGE_NAME/ID]