Skip to content

Docker_introduction

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.

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.

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 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 │ │
│ └─────────────────────────────────────────────────────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────────────────────┘
AspectVirtual MachinesDocker Containers
OSEach VM has its own OSShare host OS kernel
SizeGigabytes (GB)Megabytes (MB)
Startup TimeMinutesSeconds
IsolationComplete isolationProcess-level isolation
OverheadHigh (hypervisor)Low (shared kernel)
PortabilityModerateVery High

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) │ │
│ └──────────────────────────────────────────────────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────────────────────┘
  1. Docker Daemon (dockerd)

    • The background service running on the host
    • Manages Docker objects (images, containers, networks, volumes)
    • Listens for Docker API requests
  2. Docker Client

    • The CLI tool used to interact with Docker
    • Communicates with the daemon via Docker API
  3. Docker Registries

    • Stores Docker images
    • Docker Hub is the default public registry
    • Can use private registries (ECR, GCR, Harbor)
  4. 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 │
├─────────────────────────────────────────────────────────────────────────────┤
│ │
│ 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 │
│ │
└─────────────────────────────────────────────────────────────────────────────┘
┌─────────────────────────────────────────────────────────────────────────────┐
│ Environment Consistency │
├─────────────────────────────────────────────────────────────────────────────┤
│ │
│ Development Staging Production │
│ ┌──────────┐ ┌──────────┐ ┌──────────┐ │
│ │Container │ │Container │ │Container │ │
│ │ [App] │ │ [App] │ │ [App] │ │
│ │ ┌──────┐ │ │ ┌──────┐ │ │ ┌──────┐ │ │
│ │ │NodeJS│ │ │ │NodeJS│ │ │ │NodeJS│ │ │
│ │ └──────┘ │ │ └──────┘ │ │ └──────┘ │ │
│ │ ┌──────┐ │ │ ┌──────┐ │ │ ┌──────┐ │ │
│ │ │ npm │ │ │ │ npm │ │ │ │ npm │ │ │
│ │ └──────┘ │ │ └──────┘ │ │ └──────┘ │ │
│ └──────────┘ └──────────┘ └──────────┘ │
│ ✓ ✓ ✓ │
│ │
│ Same image everywhere! │
│ │
└─────────────────────────────────────────────────────────────────────────────┘
  • Each container runs in isolation
  • Dependencies don’t conflict
  • Multiple versions of the same software can run side by side
  • Containers share the host OS
  • Lower resource usage than VMs
  • Faster startup times (seconds vs minutes)
  • Build once, run anywhere
  • Works on Linux, macOS, Windows
  • Works on-premises and in the cloud
┌─────────────────────────────────────────────────────────────────────────────┐
│ Microservices with Docker │
├─────────────────────────────────────────────────────────────────────────────┤
│ │
│ ┌─────────────────┐ │
│ │ API Gateway │ │
│ └────────┬────────┘ │
│ │ │
│ ┌────────────────────────┼────────────────────────┐ │
│ │ │ │ │
│ ▼ ▼ ▼ │
│ ┌─────────┐ ┌─────────┐ ┌─────────┐ │
│ │ User │ │ Order │ │ Payment │ │
│ │ Service │ │ Service │ │ Service │ │
│ │[Container] │[Container] │[Container] │
│ └─────────┘ └─────────┘ └─────────┘ │
│ │ │ │ │
│ └────────────────────────┼────────────────────────┘ │
│ │ │
│ ▼ │
│ ┌─────────┐ ┌─────────┐ ┌─────────┐ ┌─────────┐ │
│ │ Redis │ │ DB │ │ MQ │ │ Cache │ │
│ │[Container]│[Container]│[Container]│[Container] │
│ └─────────┘ └─────────┘ └─────────┘ └─────────┘ │
│ │
└─────────────────────────────────────────────────────────────────────────────┘

Package applications with all dependencies into a single unit.

Use consistent containers in build, test, and deployment stages.

Deploy and scale individual services independently.

Create reproducible development environments.

Isolate tests and run them in consistent environments.

Isolate different customers or users in separate containers.

  • Community Edition (CE): Free, open-source
  • Enterprise Edition (EE): Paid, with additional features
  • Stable: Released quarterly, tested versions
  • Test: Pre-release versions for testing

See Chapter 02: Docker Installation for detailed installation instructions.

Docker is a powerful platform that revolutionizes how we develop, ship, and run applications. By using containerization:

  1. Solves the “works on my machine” problem - Consistent environments
  2. Reduces overhead - Lighter than VMs, faster startup
  3. Enables microservices - Independent deployment and scaling
  4. Improves CI/CD - Consistent builds and deployments
  5. Increases portability - Build once, run anywhere

In the next chapter, we’ll cover how to install Docker on your machine.