Docker_introduction
Chapter 01: Docker Introduction
Section titled “Chapter 01: Docker Introduction”What is Docker?
Section titled “What is Docker?”Docker is an open platform for developing, shipping, and running applications using containerization technology. It enables developers to package applications with all their dependencies into standardized units called containers.
Why Docker?
Section titled “Why Docker?”Before Docker, developers often faced the “it works on my machine” problem. Different environments (development, staging, production) had different configurations, leading to bugs that only appeared in production.
┌─────────────────────────────────────────────────────────────────────────────┐│ The "It Works on My Machine" Problem │├─────────────────────────────────────────────────────────────────────────────┤│ ││ Developer Machine Staging Server Production Server ││ ┌────────────────┐ ┌────────────────┐ ┌────────────────┐ ││ │ Node.js 16 │ │ Node.js 14 │ │ Node.js 12 │ ││ │ Ubuntu 22.04 │ │ Ubuntu 20.04 │ │ Ubuntu 18.04 │ ││ │ Python 3.10 │ ≠ │ Python 3.8 │ ≠ │ Python 3.6 │ ││ │ Redis 7.0 │ │ Redis 6.0 │ │ Redis 5.0 │ ││ └────────────────┘ └────────────────┘ └────────────────┘ ││ ││ Result: "But it worked locally!" ││ │└─────────────────────────────────────────────────────────────────────────────┘Docker solves this by creating identical containers across all environments.
Docker vs Virtual Machines
Section titled “Docker vs Virtual Machines”Traditional Virtual Machines
Section titled “Traditional Virtual Machines”Traditional VMs virtualize the entire hardware stack - each VM includes:
- A full operating system
- Virtualized hardware (CPU, RAM, storage, network)
- Application and its dependencies
┌─────────────────────────────────────────────────────────────────────────────┐│ Virtual Machine Architecture │├─────────────────────────────────────────────────────────────────────────────┤│ ││ ┌─────────────────────────────────────────────────────────────────────┐ ││ │ Hypervisor (VMware, Hyper-V) │ ││ ├───────────────┬───────────────┬───────────────┬───────────────────┤ ││ │ VM 1 │ VM 2 │ VM 3 │ VM 4 │ ││ │ ┌───────────┐ │ ┌───────────┐ │ ┌───────────┐ │ ┌────────────┐ │ ││ │ │ App A │ │ │ App B │ │ │ App C │ │ │ App D │ │ ││ │ ├───────────┤ │ ├───────────┤ │ ├───────────┤ │ ├────────────┤ │ ││ │ │ Runtime │ │ │ Runtime │ │ │ Runtime │ │ │ Runtime │ │ ││ │ ├───────────┤ │ ├───────────┤ │ ├───────────┤ │ ├────────────┤ │ ││ │ │ OS │ │ │ OS │ │ │ OS │ │ │ OS │ │ ││ │ └───────────┘ │ └───────────┘ │ └───────────┘ │ └────────────┘ │ ││ └───────────────┴───────────────┴───────────────┴───────────────────┘ ││ ││ ┌──────────────────────────────────────────────────────────────── ││ │ ─────┐ Physical Hardware │ ││ │ CPU | Memory | Storage | Network │ ││ └─────────────────────────────────────────────────────────────────────┘ ││ │└─────────────────────────────────────────────────────────────────────────────┘Docker Containers
Section titled “Docker Containers”Docker containers share the host OS kernel but maintain isolation between containers. Each container includes:
- Application and its dependencies
- Minimal runtime environment
- Filesystem layers
┌─────────────────────────────────────────────────────────────────────────────┐│ Docker Container Architecture │├─────────────────────────────────────────────────────────────────────────────┤│ ││ ┌─────────────────────────────────────────────────────────────────────┐ ││ │ Docker Engine │ ││ ├───────────────────────┬───────────────────────┬─────────────────────┤ ││ │ Container A │ Container B │ Container C │ ││ │ ┌─────────────────┐ │ ┌─────────────────┐ │ ┌────────────────┐ │ ││ │ │ App A │ │ │ App B │ │ │ App C │ │ ││ │ ├─────────────────┤ │ ├─────────────────┤ │ ├────────────────┤ │ ││ │ │ Libraries │ │ │ Libraries │ │ │ Libraries │ │ ││ │ ├─────────────────┤ │ ├─────────────────┤ │ ├────────────────┤ │ ││ │ │ App Image │ │ │ App Image │ │ │ App Image │ │ ││ │ └─────────────────┘ │ └─────────────────┘ │ └────────────────┘ │ ││ └───────────────────────┴───────────────────────┴─────────────────────┘ ││ ││ ┌─────────────────────────────────────────────────────────────────────┐ ││ │ Host Operating System (Linux) │ ││ └─────────────────────────────────────────────────────────────────────┘ ││ ││ ┌─────────────────────────────────────────────────────────────────────┐ ││ │ Physical Hardware │ ││ └─────────────────────────────────────────────────────────────────────┘ ││ │└─────────────────────────────────────────────────────────────────────────────┘Key Differences
Section titled “Key Differences”| Aspect | Virtual Machines | Docker Containers |
|---|---|---|
| OS | Each VM has its own OS | Share host OS kernel |
| Size | Gigabytes (GB) | Megabytes (MB) |
| Startup Time | Minutes | Seconds |
| Isolation | Complete isolation | Process-level isolation |
| Overhead | High (hypervisor) | Low (shared kernel) |
| Portability | Moderate | Very High |
Docker Architecture
Section titled “Docker Architecture”Docker uses a client-server architecture:
┌─────────────────────────────────────────────────────────────────────────────┐│ Docker Architecture │├─────────────────────────────────────────────────────────────────────────────┤│ ││ ┌──────────────────────────────────────────────────────────────────┐ ││ │ Docker Client │ ││ │ (docker CLI, REST API) │ ││ └────────────────────────────┬─────────────────────────────────────┘ ││ │ ││ │ ││ ┌────────────────────────────▼─────────────────────────────────────┐ ││ │ Docker Daemon (dockerd) │ ││ │ │ ││ │ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │ ││ │ │ Builder │ │ Packager │ │ Runner │ │ ││ │ │ (images) │ │ (images) │ │(containers) │ │ ││ │ └─────────────┘ └─────────────┘ └─────────────┘ │ ││ │ │ ││ │ ┌─────────────────────────────────────────────────────────┐ │ ││ │ │ Container Runtime (runc, containerd) │ │ ││ │ └─────────────────────────────────────────────────────────┘ │ ││ │ │ ││ └────────────────────────────┬─────────────────────────────────────┘ ││ │ ││ ┌────────────────────────────▼─────────────────────────────────────┐ ││ │ Docker Registry │ ││ │ (Docker Hub, ECR, GCR, Harbor) │ ││ └──────────────────────────────────────────────────────────────────┘ ││ │└─────────────────────────────────────────────────────────────────────────────┘Components
Section titled “Components”-
Docker Daemon (dockerd)
- The background service running on the host
- Manages Docker objects (images, containers, networks, volumes)
- Listens for Docker API requests
-
Docker Client
- The CLI tool used to interact with Docker
- Communicates with the daemon via Docker API
-
Docker Registries
- Stores Docker images
- Docker Hub is the default public registry
- Can use private registries (ECR, GCR, Harbor)
-
Docker Objects
- Images: Read-only templates for creating containers
- Containers: Runnable instances of images
- Networks: Enable communication between containers
- Volumes: Persistent data storage
Docker Workflow
Section titled “Docker Workflow”┌─────────────────────────────────────────────────────────────────────────────┐│ Docker Workflow │├─────────────────────────────────────────────────────────────────────────────┤│ ││ 1. BUILD 2. PUSH 3. PULL ││ ┌───────────┐ ┌───────────┐ ┌───────────┐ ││ │ Develop │────build────▶│ Registry │◀──push───│ Registry │ ││ │ App │ │ (Hub) │ │ (Hub) │ ││ └───────────┘ └───────────┘ └─────┬─────┘ ││ │ ││ Dockerfile │ ││ ┌────────────────┐ │ ││ │ FROM node:18 │ │ ││ │ WORKDIR /app │ │ ││ │ COPY . . │ │ ││ │ RUN npm install │ ││ │ CMD ["npm", "start"] │ ││ └────────────────┘ │ ││ │ ││ │ ││ 6. DEPLOY 5. RUN 4. PULL │ ││ ┌───────────┐ ┌───────────┐ ┌────────▼─────┐ ││ │ Server/ │◀──run────────│ Docker │◀─────│ Server/ │ ││ │ Cloud │ │ Engine │ │ Cloud │ ││ └───────────┘ └───────────┘ └───────────────┘ ││ ││ Image Lifecycle: Build → Push → Pull → Run → Deploy ││ │└─────────────────────────────────────────────────────────────────────────────┘Benefits of Docker
Section titled “Benefits of Docker”1. Consistency Across Environments
Section titled “1. Consistency Across Environments”┌─────────────────────────────────────────────────────────────────────────────┐│ Environment Consistency │├─────────────────────────────────────────────────────────────────────────────┤│ ││ Development Staging Production ││ ┌──────────┐ ┌──────────┐ ┌──────────┐ ││ │Container │ │Container │ │Container │ ││ │ [App] │ │ [App] │ │ [App] │ ││ │ ┌──────┐ │ │ ┌──────┐ │ │ ┌──────┐ │ ││ │ │NodeJS│ │ │ │NodeJS│ │ │ │NodeJS│ │ ││ │ └──────┘ │ │ └──────┘ │ │ └──────┘ │ ││ │ ┌──────┐ │ │ ┌──────┐ │ │ ┌──────┐ │ ││ │ │ npm │ │ │ │ npm │ │ │ │ npm │ │ ││ │ └──────┘ │ │ └──────┘ │ │ └──────┘ │ ││ └──────────┘ └──────────┘ └──────────┘ ││ ✓ ✓ ✓ ││ ││ Same image everywhere! ││ │└─────────────────────────────────────────────────────────────────────────────┘2. Isolation
Section titled “2. Isolation”- Each container runs in isolation
- Dependencies don’t conflict
- Multiple versions of the same software can run side by side
3. Efficiency
Section titled “3. Efficiency”- Containers share the host OS
- Lower resource usage than VMs
- Faster startup times (seconds vs minutes)
4. Portability
Section titled “4. Portability”- Build once, run anywhere
- Works on Linux, macOS, Windows
- Works on-premises and in the cloud
5. Microservices Architecture
Section titled “5. Microservices Architecture”┌─────────────────────────────────────────────────────────────────────────────┐│ Microservices with Docker │├─────────────────────────────────────────────────────────────────────────────┤│ ││ ┌─────────────────┐ ││ │ API Gateway │ ││ └────────┬────────┘ ││ │ ││ ┌────────────────────────┼────────────────────────┐ ││ │ │ │ ││ ▼ ▼ ▼ ││ ┌─────────┐ ┌─────────┐ ┌─────────┐ ││ │ User │ │ Order │ │ Payment │ ││ │ Service │ │ Service │ │ Service │ ││ │[Container] │[Container] │[Container] ││ └─────────┘ └─────────┘ └─────────┘ ││ │ │ │ ││ └────────────────────────┼────────────────────────┘ ││ │ ││ ▼ ││ ┌─────────┐ ┌─────────┐ ┌─────────┐ ┌─────────┐ ││ │ Redis │ │ DB │ │ MQ │ │ Cache │ ││ │[Container]│[Container]│[Container]│[Container] ││ └─────────┘ └─────────┘ └─────────┘ └─────────┘ ││ │└─────────────────────────────────────────────────────────────────────────────┘Docker Use Cases
Section titled “Docker Use Cases”1. Application Packaging
Section titled “1. Application Packaging”Package applications with all dependencies into a single unit.
2. CI/CD Pipelines
Section titled “2. CI/CD Pipelines”Use consistent containers in build, test, and deployment stages.
3. Microservices
Section titled “3. Microservices”Deploy and scale individual services independently.
4. Development Environments
Section titled “4. Development Environments”Create reproducible development environments.
5. Testing
Section titled “5. Testing”Isolate tests and run them in consistent environments.
6. Multi-tenant Applications
Section titled “6. Multi-tenant Applications”Isolate different customers or users in separate containers.
Docker Versions and Editions
Section titled “Docker Versions and Editions”Docker Engine
Section titled “Docker Engine”- Community Edition (CE): Free, open-source
- Enterprise Edition (EE): Paid, with additional features
Version Naming
Section titled “Version Naming”- Stable: Released quarterly, tested versions
- Test: Pre-release versions for testing
Installing Docker
Section titled “Installing Docker”See Chapter 02: Docker Installation for detailed installation instructions.
Summary
Section titled “Summary”Docker is a powerful platform that revolutionizes how we develop, ship, and run applications. By using containerization:
- Solves the “works on my machine” problem - Consistent environments
- Reduces overhead - Lighter than VMs, faster startup
- Enables microservices - Independent deployment and scaling
- Improves CI/CD - Consistent builds and deployments
- Increases portability - Build once, run anywhere
In the next chapter, we’ll cover how to install Docker on your machine.