Docker Architecture Basics

Let's understand HOW Docker actually works behind the scenes. What are the different components and how do they work together?


Overview - The Big Picture

Simple Analogy First:

Think of Docker like a Restaurant System:

Restaurant (Docker System):
│
├── Customer (You/Developer)
│   └── Orders food (runs Docker commands)
│
├── Waiter (Docker Client)
│   └── Takes your order, brings food back
│
├── Kitchen Manager (Docker Daemon)
│   └── Receives orders, manages cooking
│
├── Chefs (Docker Engine)
│   └── Actually cook the food (run containers)
│
└── Food Supplier (Docker Registry/Hub)
    └── Provides ingredients (provides images)

Now let's understand each component in detail!


Component 1: Docker Client

What is Docker Client?

Simple Definition:

Docker Client = The interface you use to talk to Docker. It's like a remote control for Docker.

What It Does:

You (Developer):
├── Type commands on keyboard
├── "docker run nginx"
├── "docker build -t myapp ."
└── "docker ps"
        ↓
Docker Client:
├── Takes your commands
├── Translates them
├── Sends to Docker Daemon
└── Shows you the results

Real-Life Example:

Think of TV Remote Control:
│
├── You press buttons (give commands)
│       ↓
├── Remote sends signals (Docker Client)
│       ↓
├── TV receives signals (Docker Daemon)
│       ↓
└── TV changes channel (Action happens)

You don't directly touch the TV,
you use the remote!

Docker Client in Action

When you type a command:

$ docker run hello-world

What happens:

Step 1: You type in Terminal
Command: docker run hello-world
        ↓
Step 2: Docker Client receives it
Client thinks: "User wants to run 'hello-world' container"
        ↓
Step 3: Client sends request to Docker Daemon
Client says: "Hey Daemon, please run hello-world container"
        ↓
Step 4: Daemon does the work
Daemon runs the container
        ↓
Step 5: Client shows you the result
Output: "Hello from Docker!"

Visual Diagram:

┌─────────────────┐
│   Your Terminal │
│                 │
│  $ docker run   │
│    hello-world  │
└────────┬────────┘
         │
         │ Command
         ↓
┌─────────────────┐
│  Docker Client  │
│                 │
│  - Parses cmd   │
│  - Validates    │
│  - Sends to     │
│    daemon       │
└────────┬────────┘
         │
         │ API Call
         ↓
┌─────────────────┐
│  Docker Daemon  │
│                 │
│  - Receives     │
│  - Executes     │
│  - Returns      │
│    result       │
└────────┬────────┘
         │
         │ Result
         ↓
┌─────────────────┐
│   Your Terminal │
│                 │
│  Output shown   │
└─────────────────┘

Important Points About Docker Client

1. The CLI (Command Line Interface):

This is the Docker Client:
$ docker <command>

Examples:
$ docker run nginx        ← Docker Client command
$ docker ps              ← Docker Client command
$ docker build .         ← Docker Client command
$ docker stop myapp      ← Docker Client command

2. Can Be Remote:

Docker Client can be on different computer:

Your Laptop (Client):
$ docker run myapp
        ↓
        │ Internet
        ↓
Remote Server (Daemon):
└── Actually runs the container

You control remote Docker from your laptop!

3. Different Interfaces:

Ways to use Docker Client:
├── Command Line (Terminal) ← Most common
├── Docker Desktop (GUI)    ← Visual interface
├── Docker API (Code)       ← From programs
└── Third-party tools       ← Portainer, etc.

All talk to Docker Daemon!

Component 2: Docker Daemon (dockerd)

What is Docker Daemon?

Simple Definition:

Docker Daemon = The background service that does all the actual work. The "brain" of Docker.

What It Does:

Docker Daemon (Background Process):
├── Listens for commands from Client
├── Manages containers (create, start, stop)
├── Manages images (build, pull, push)
├── Manages networks
├── Manages volumes
└── Does ALL the heavy lifting!

Real-Life Example:

Think of a Power Plant:
│
├── You flip light switch (Docker Client)
│       ↓
├── Signal goes to power plant (Docker Daemon)
│       ↓
├── Power plant generates electricity
│       ↓
└── Your light turns on

You don't see the power plant working,
but it's doing all the work!

Docker Daemon Responsibilities

1. Container Lifecycle Management:

Docker Daemon manages:

Creating containers:
$ docker run nginx
        ↓
Daemon: "I'll create nginx container"
        ↓
Container created ✓

Starting/Stopping:
$ docker stop nginx
        ↓
Daemon: "I'll stop nginx container"
        ↓
Container stopped ✓

Removing:
$ docker rm nginx
        ↓
Daemon: "I'll remove nginx container"
        ↓
Container removed ✓

2. Image Management:

Docker Daemon handles:

Pulling images:
$ docker pull ubuntu
        ↓
Daemon: "I'll download ubuntu from Docker Hub"
        ↓
Image downloaded ✓

Building images:
$ docker build -t myapp .
        ↓
Daemon: "I'll read Dockerfile and build image"
        ↓
Image built ✓

Storing images:
Daemon keeps all images on disk
Ready for use anytime

3. Network Management:

Docker Daemon creates networks:

Default network:
Daemon automatically creates bridge network

Custom networks:
$ docker network create mynetwork
        ↓
Daemon creates isolated network ✓

Connecting containers:
Daemon connects containers to networks
So they can talk to each other

4. Volume Management:

Docker Daemon manages storage:

Creating volumes:
$ docker volume create mydata
        ↓
Daemon creates storage space ✓

Mounting volumes:
$ docker run -v mydata:/app/data nginx
        ↓
Daemon mounts volume to container ✓

Docker Daemon Process

Where It Runs:

Background Process (Always Running):

Linux:
systemctl status docker
● docker.service - Docker Application Container Engine
   Active: active (running)

Windows/Mac (Docker Desktop):
Docker Desktop app manages daemon
Daemon runs in VM in background

Check if running:
$ docker info
If you see output, daemon is running ✓

How It Communicates:

Docker Daemon listens on:

Unix Socket (Local):
/var/run/docker.sock
        ↑
Docker Client connects here (by default)

TCP Port (Remote):
Port 2375 (unsecured) or 2376 (secured)
        ↑
Remote clients can connect here

Component 3: Docker Engine

What is Docker Engine?

Simple Definition:

Docker Engine = The complete Docker system. It includes the Daemon plus all the underlying technology that makes containers work.

Think of it as:

Docker Engine = Complete Package:
│
├── Docker Daemon (Main process)
├── containerd (Container runtime)
├── runc (Low-level container executor)
└── All supporting components

Like a car engine:
├── Pistons (Daemon)
├── Fuel system (containerd)
├── Spark plugs (runc)
└── Everything working together

Docker Engine Components (Detailed)

The Layers:

                    ┌──────────────────┐
                    │  Docker Client   │
                    └────────┬─────────┘
                             │
                             ↓
┌─────────────────────────────────────────────┐
│          Docker Engine                       │
│                                              │
│  ┌────────────────────────────────────┐    │
│  │      Docker Daemon (dockerd)        │    │
│  │  - High-level operations            │    │
│  │  - API server                       │    │
│  │  - Image management                 │    │
│  └──────────────┬─────────────────────┘    │
│                 │                            │
│                 ↓                            │
│  ┌────────────────────────────────────┐    │
│  │      containerd                     │    │
│  │  - Container lifecycle              │    │
│  │  - Image distribution               │    │
│  └──────────────┬─────────────────────┘    │
│                 │                            │
│                 ↓                            │
│  ┌────────────────────────────────────┐    │
│  │      runc                           │    │
│  │  - Actually creates containers      │    │
│  │  - Low-level operations             │    │
│  └─────────────────────────────────────┘   │
│                                              │
└──────────────────┬───────────────────────────┘
                   │
                   ↓
            ┌──────────────┐
            │  Containers   │
            └──────────────┘

What Each Layer Does:

1. Docker Daemon (dockerd) - Top Layer:

High-level manager:
├── Receives commands from Client
├── "User wants to run nginx"
├── Passes request down to containerd
└── Returns results to Client

2. containerd - Middle Layer:

Container supervisor:
├── Receives from dockerd
├── "Okay, I'll manage nginx container"
├── Handles image pulling
├── Passes to runc for actual creation
└── Monitors container lifecycle

3. runc - Bottom Layer:

Container creator:
├── Receives from containerd
├── "I'll create the actual container now"
├── Uses Linux kernel features (namespaces, cgroups)
├── Actually spawns the container process
└── Container is now running!

Example Flow - Starting a Container

When you run:

$ docker run nginx

Complete flow through Docker Engine:

Step 1: Docker Client
You type: docker run nginx
Client sends: "Run nginx container" to Daemon

Step 2: Docker Daemon (dockerd)
Daemon receives: "Run nginx container"
Daemon checks: "Do I have nginx image?"
        ├─ Yes → Continue to step 3
        └─ No → Download from Docker Hub first

Step 3: Daemon → containerd
Daemon tells containerd: "Create nginx container"
containerd prepares: Image, network, volumes

Step 4: containerd → runc
containerd tells runc: "Spawn the container process"
runc creates: Linux namespaces, cgroups
runc starts: nginx process inside container

Step 5: Container Running
nginx container is now running! ✓
        ↓
Result sent back up:
runc → containerd → dockerd → client → you

You see: "Container started successfully"

Component 4: Docker Registry (Docker Hub)

What is Docker Registry?

Simple Definition:

Docker Registry = A storage place for Docker images. Like GitHub for code, but for Docker images.

Simple Analogy:

Think of it like an App Store:

Apple App Store:
├── Stores apps
├── You download apps
├── Developers upload apps
└── Everyone shares apps

Docker Registry (Docker Hub):
├── Stores Docker images
├── You download (pull) images
├── Developers upload (push) images
└── Everyone shares images

Docker Hub (Default Registry)

What is Docker Hub?

Docker Hub = Official Docker Registry:
├── hub.docker.com
├── Free public registry
├── Millions of images available
├── Official images from companies
└── Community images from developers

Popular Images on Docker Hub:

Official Images:
├── nginx - Web server
├── mysql - Database
├── python - Python environment
├── node - Node.js environment
├── ubuntu - Ubuntu OS
├── redis - Cache database
└── postgres - Database

All free to download and use!

How Registry Works

1. Pulling Images (Downloading):

You want nginx image:

$ docker pull nginx
        ↓
Docker Client: "Get nginx from registry"
        ↓
Docker Daemon: "I'll download it"
        ↓
Docker Hub (Registry):
└── "Here's nginx image" → Downloads to your computer
        ↓
Stored locally: Ready to use!

Now you can run:
$ docker run nginx

Visual Flow:

┌─────────────────────┐
│   Docker Hub        │
│   (Registry)        │
│                     │
│  • nginx image      │
│  • ubuntu image     │
│  • python image     │
└──────────┬──────────┘
           │
           │ docker pull nginx
           ↓
┌─────────────────────┐
│   Your Computer     │
│                     │
│  • nginx image ✓    │ ← Downloaded
│                     │
│  Can now run:       │
│  docker run nginx   │
└─────────────────────┘

2. Pushing Images (Uploading):

You created custom image:

$ docker build -t myusername/myapp .
        ↓
Image built locally ✓

$ docker push myusername/myapp
        ↓
Docker Daemon: "I'll upload to registry"
        ↓
Docker Hub (Registry):
└── "myapp received" → Stored on Docker Hub
        ↓
Now others can download:
$ docker pull myusername/myapp

Registry Types

1. Docker Hub (Public):

├── Free tier available
├── Public images (anyone can download)
├── Private images (paid, only you can access)
└── Most commonly used

2. Private Registries:

Companies run their own:
├── Amazon ECR (AWS)
├── Google Container Registry
├── Azure Container Registry
├── Self-hosted registries
└── For private/company images

3. Alternative Registries:

├── Quay.io
├── GitHub Container Registry
└── GitLab Container Registry

How All Components Work Together

Complete Flow Example

Scenario: You want to run nginx web server

STEP 1: You give command
┌──────────────────┐
│  Your Terminal   │
│                  │
│ $ docker run     │
│   nginx          │
└────────┬─────────┘
         │
         │ ① Command typed
         ↓

STEP 2: Docker Client processes
┌──────────────────┐
│  Docker Client   │
│                  │
│ • Parses command │
│ • Validates      │
│ • Sends to       │
│   daemon         │
└────────┬─────────┘
         │
         │ ② API request
         ↓

STEP 3: Docker Daemon checks
┌──────────────────┐
│  Docker Daemon   │
│                  │
│ "Do I have       │
│  nginx image?"   │
│                  │
│ • Checks local   │
│   storage        │
└────────┬─────────┘
         │
         │ ③ Image check
         ↓

STEP 4: If not found, pull from registry
┌──────────────────┐
│  Docker Hub      │
│  (Registry)      │
│                  │
│ • Sends nginx    │
│   image          │
└────────┬─────────┘
         │
         │ ④ Download image
         ↓

STEP 5: Daemon tells containerd
┌──────────────────┐
│  containerd      │
│                  │
│ • Prepares       │
│   container      │
│ • Sets up        │
│   resources      │
└────────┬─────────┘
         │
         │ ⑤ Create request
         ↓

STEP 6: runc creates container
┌──────────────────┐
│  runc            │
│                  │
│ • Uses Linux     │
│   features       │
│ • Spawns         │
│   process        │
└────────┬─────────┘
         │
         │ ⑥ Container created
         ↓

STEP 7: Container running!
┌──────────────────┐
│  nginx Container │
│                  │
│ • Running        │
│ • Serving web    │
│   pages          │
└──────────────────┘

Architecture Summary Diagram

                    YOU (Developer)
                         │
                         │ Types commands
                         ↓
        ┌────────────────────────────────┐
        │      DOCKER CLIENT             │
        │  (CLI, Desktop, API)           │
        └────────────┬───────────────────┘
                     │
                     │ REST API calls
                     ↓
        ┌────────────────────────────────┐
        │      DOCKER DAEMON             │
        │  - High-level management       │
        │  - API server                  │
        │  - Image management            │
        └────────────┬───────────────────┘
                     │
           ┌─────────┴─────────┐
           │                   │
           ↓                   ↓
    ┌──────────┐      ┌───────────────┐
    │containerd│      │  DOCKER HUB   │
    │          │      │  (Registry)   │
    │ Container│←─────│  Image Store  │
    │ Runtime  │ Pull │               │
    └────┬─────┘      └───────────────┘
         │
         ↓
    ┌─────────┐
    │  runc   │
    │         │
    │ Creates │
    │Container│
    └────┬────┘
         │
         ↓
    ┌─────────────────────────────┐
    │     CONTAINERS              │
    │  ┌────┐ ┌────┐ ┌────┐      │
    │  │ C1 │ │ C2 │ │ C3 │      │
    │  └────┘ └────┘ └────┘      │
    └─────────────────────────────┘
         │
         ↓
    ┌─────────────────────────────┐
    │     HOST OPERATING SYSTEM   │
    │     (Linux Kernel)          │
    └─────────────────────────────┘
         │
         ↓
    ┌─────────────────────────────┐
    │     HARDWARE                │
    │  (CPU, RAM, Disk, Network)  │
    └─────────────────────────────┘

Key Takeaways

Four Main Components:

1. Docker Client
   └── Your interface to Docker (CLI/GUI)

2. Docker Daemon
   └── The background service doing the work

3. Docker Engine
   └── Complete system (Daemon + containerd + runc)

4. Docker Registry
   └── Storage for Docker images (Docker Hub)

How They Work Together:

You → Client → Daemon → Engine → Container
                 ↕
              Registry (for images)

Remember:

  • Client = What you interact with
  • Daemon = The brain doing the work
  • Engine = The complete machinery
  • Registry = Where images are stored

No comments:

Post a Comment

Docker Architecture Basics

Let's understand HOW Docker actually works behind the scenes. What are the different components and how do they work together? Overview ...