Day 6:Virtual Machine & Containerization(Docker Beginning)

Aditya TiwaryAditya Tiwary
5 min read

✅ What is a VM (Virtual Machine)?

A Virtual Machine is a software-based simulation of a physical computer. It runs on a hypervisor like VMware, VirtualBox, or KVM, which sits on top of the host OS or hardware.

  • Each VM has its own OS, libraries, and applications.

  • It uses virtual hardware (CPU, memory, storage, etc.)

  • VMs are isolated from one another.

🖼 Example:
Running Ubuntu and Windows simultaneously on a macOS laptop via VirtualBox.


✅ What is Containerization?

Containerization is a lightweight form of virtualization using containers, which share the host system’s kernel but have isolated environments.

  • Containers are created using tools like Docker or Podman.

  • Containers share the host OS but have their own filesystem, libraries, and dependencies.

  • They are much more lightweight and faster to start than VMs.

🖼 Example:
Running multiple Python apps in isolated Docker containers on the same host OS.


✅ Use Cases

Use CaseVMContainer
Run different OSes✅ Yes (e.g., Linux on Windows)❌ No (shares host OS kernel)
Legacy application✅ Yes⚠️ Sometimes (depending on dependencies)
Microservices deployment❌ Heavy✅ Ideal for microservices
Development & Testing✅ Commonly used✅ Faster and more efficient

✅ Advantages of VMs over Containers

AdvantageWhy it matters
Strong IsolationVMs don't share OS kernel, offering better security
Different OS SupportVMs can run any OS (Linux, Windows, etc.)
Better for Monolithic AppsUseful for large, legacy applications needing full OS
Mature EcosystemVM tools and platforms have existed longer (e.g., VMware)

✅Architecture Of Docker

Here’s a clear and concise explanation of the Docker architecture, including its components and how they work together.


image

🐳 Docker Architecture Overview

Docker uses a client-server architecture consisting of:

  1. Docker Client

  2. Docker Daemon (Server)

  3. Docker Objects (Images, Containers, Volumes, etc.)

  4. Docker Registry (e.g., Docker Hub)


📌 1. Docker Client (docker command)

  • It's what you interact with via terminal or scripts.

  • Sends commands like docker build, docker run, docker pull to the Docker Daemon.

  • Communicates via REST API over UNIX socket or network.

🧠 Think of it as the "front-end" of Docker.


📌 2. Docker Daemon (dockerd)

  • It's the main engine of Docker.

  • Listens for API requests from the Docker client.

  • Manages:

    • Containers

    • Images

    • Volumes

    • Networks

  • Runs as a background service on your system.

🧠 Think of it as the "brain" or "backend" of Docker.


📌 3. Docker Objects

🖼 a. Images

  • Blueprint or snapshot for containers.

  • Built from Dockerfile.

  • Immutable and layered.

📦 b. Containers

  • Running instances of images.

  • Lightweight and isolated.

  • Share the host OS kernel.

📂 c. Volumes

  • Persistent storage for containers.

  • Useful when container is deleted and data must persist.

🌐 d. Networks

  • Used for communication between containers or external world.

📌 4. Docker Registry

  • Storage and distribution system for Docker images.

  • Default: Docker Hub

  • You can also use private registries (like AWS ECR, GitHub Container Registry, Harbor).

Common commands:

  • docker pull nginx → Pulls image from registry

  • docker push my-image → Pushes image to registry


🔁 Flow Diagram (Textual)

scssCopyEditUser
 ↓
Docker CLI (Client)
 ↓  (REST API)
Docker Daemon (dockerd)
 ├── Image Management
 ├── Container Management
 ├── Volume Management
 └── Network Management
 ↓
Docker Registry (e.g., Docker Hub)

🧩 Example Flow

You run:

bashCopyEditdocker run nginx

What happens:

  1. Docker Client sends request to Daemon

  2. Daemon checks for nginx image

  3. If not present, it pulls from Docker Hub

  4. Daemon creates and runs the container from the image

  5. Container is isolated but shares host kernel


.


🚦 Docker Container Lifecycle

Screenshot 2023-02-08 at 4 32 13 PM

1️⃣ Created

  • The container is created but not running yet.

  • It’s based on an image and has a container ID assigned.

🛠️ Command:

bashCopyEditdocker create nginx

⏳ State:

  • Container exists on disk.

  • Not consuming CPU or memory.


2️⃣ Running

  • The container is actively executing the process inside.

  • It’s isolated but shares the host OS kernel.

🛠️ Command:

bashCopyEditdocker run nginx

(Equivalent to docker create + docker start)

🟢 Status:

bashCopyEditdocker ps

3️⃣ Paused (optional)

  • Process is temporarily frozen (e.g., for maintenance).

  • Container remains in memory.

🛠️ Command:

bashCopyEditdocker pause <container_id>
docker unpause <container_id>

4️⃣ Stopped (Exited)

  • Container process has stopped (gracefully or forcibly).

  • Container still exists, so you can inspect logs or restart it.

🛠️ Commands:

bashCopyEditdocker stop <container_id>     # Graceful
docker kill <container_id>     # Force stop

📦 View stopped containers:

bashCopyEditdocker ps -a

5️⃣ Restarted (optional)

  • Container is restarted after being stopped.

🛠️ Command:

bashCopyEditdocker restart <container_id>

You can also use --restart policy like:

bashCopyEditdocker run --restart=always nginx

6️⃣ Removed

  • Container is completely deleted.

  • No logs, metadata, or filesystem remain.

🛠️ Commands:

bashCopyEditdocker rm <container_id>
docker container prune   # Removes all stopped containers

If container is still running:

bashCopyEditdocker rm -f <container_id>

🔁 Lifecycle Summary Table

StateDescriptionCommand
CreatedContainer created but not starteddocker create
RunningActive container running a processdocker run / docker start
PausedExecution frozendocker pause
StoppedProcess exiteddocker stop / docker kill
RestartedRe-run stopped containerdocker restart
RemovedDeleted from systemdocker rm
0
Subscribe to my newsletter

Read articles from Aditya Tiwary directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Aditya Tiwary
Aditya Tiwary