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
| Container | Virtual Machine | |
|---|---|---|
| Boot time | Seconds | Minutes |
| Size | MBs | GBs |
| Isolation | Process-level (shared kernel) | Full OS (own kernel) |
| Performance | Near-native | Overhead (hypervisor) |
| Density | 100s per host | 10s per host |
| Use case | Microservices, CI/CD | Legacy 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 │ │
│ └───────────┘ │
└──────────────────────────────────────────────┘
| Concept | Description |
|---|---|
| Image | Read-only template (blueprint) for creating containers |
| Container | Running instance of an image |
| Dockerfile | Instructions to build an image |
| Registry | Storage for images (Docker Hub, ECR, GCR) |
| Volume | Persistent storage outside container lifecycle |
| Network | Communication 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
-dfor detached mode,-pfor port mapping,-efor environment variables - Use
-vfor persistent volumes (data survives container restarts) docker exec -it <id> shto debug inside a running containerdocker system pruneto reclaim disk space