Docs
/
Docker Kubernetes
Chapter 1

01 — Docker Fundamentals

What is Docker?

Docker packages applications and their dependencies into containers — lightweight, portable, isolated environments that run consistently everywhere.

Traditional:                    Docker:
App → depends on OS libs       App + dependencies → Container
"Works on my machine" 😢       "Works everywhere" ✅

Containers vs VMs

ContainerVirtual Machine
Boot timeSecondsMinutes
SizeMBsGBs
IsolationProcess-level (shared kernel)Full OS (own kernel)
PerformanceNear-nativeOverhead (hypervisor)
Density100s per host10s per host
Use caseMicroservices, CI/CDLegacy apps, full OS isolation
VM:                          Container:
┌─────────┐ ┌─────────┐    ┌─────┐ ┌─────┐ ┌─────┐
│  App A  │ │  App B  │    │App A│ │App B│ │App C│
│  Libs   │ │  Libs   │    │Libs │ │Libs │ │Libs │
│ Guest OS│ │ Guest OS│    └──┬──┘ └──┬──┘ └──┬──┘
└────┬────┘ └────┬────┘       │      │      │
     │           │         ┌──┴──────┴──────┴──┐
┌────┴───────────┴────┐    │   Docker Engine    │
│    Hypervisor       │    └────────┬───────────┘
└─────────┬───────────┘             │
     Host OS                   Host OS

Docker Architecture

┌──────────────────────────────────────────────┐
│                Docker Client (CLI)            │
│  docker build, docker run, docker pull       │
└─────────────────────┬────────────────────────┘
                      │ REST API
┌─────────────────────┴────────────────────────┐
│              Docker Daemon (dockerd)          │
│                                               │
│  ┌───────────┐  ┌───────────┐  ┌───────────┐│
│  │  Images   │  │Containers │  │  Volumes   ││
│  └───────────┘  └───────────┘  └───────────┘│
│  ┌───────────┐                               │
│  │ Networks  │                               │
│  └───────────┘                               │
└──────────────────────────────────────────────┘
ConceptDescription
ImageRead-only template (blueprint) for creating containers
ContainerRunning instance of an image
DockerfileInstructions to build an image
RegistryStorage for images (Docker Hub, ECR, GCR)
VolumePersistent storage outside container lifecycle
NetworkCommunication between containers

Essential Commands

# Images
docker pull nginx                    # Download image
docker images                        # List images
docker rmi nginx                     # Remove image
docker build -t my-app .             # Build from Dockerfile

# Containers
docker run -d -p 3000:3000 my-app   # Run detached, map port
docker ps                            # List running containers
docker ps -a                         # List all (including stopped)
docker stop <id>                     # Stop container
docker rm <id>                       # Remove container
docker logs <id>                     # View logs
docker exec -it <id> sh             # Shell into container

# Cleanup
docker system prune                  # Remove unused data
docker system prune -a               # Remove all unused images too

Running Your First Container

# Run Nginx web server
docker run -d -p 8080:80 --name web nginx
# Visit http://localhost:8080

# Run Node.js app
docker run -d -p 3000:3000 --name api \
  -e NODE_ENV=production \
  -e DATABASE_URL=postgres://... \
  my-node-app

# Run PostgreSQL
docker run -d -p 5432:5432 --name db \
  -e POSTGRES_PASSWORD=secret \
  -e POSTGRES_DB=myapp \
  -v pgdata:/var/lib/postgresql/data \
  postgres:16

# Run Redis
docker run -d -p 6379:6379 --name cache redis:7-alpine

Key Takeaways

  • Docker containers are lightweight, portable, and start in seconds
  • Images are immutable blueprints; containers are running instances
  • Use -d for detached mode, -p for port mapping, -e for environment variables
  • Use -v for persistent volumes (data survives container restarts)
  • docker exec -it <id> sh to debug inside a running container
  • docker system prune to reclaim disk space