Keyboard Shortcuts N Next post
P Previous post
S Save / unsave
R Read aloud
T Toggle theme
/ Focus search
Esc Close panels
🔥
Ready to read...
Docker Docker & Containers: From Zero to Production Docker Basics Module 1 — Container Fundamentals virtualization

Containers vs Virtual Machines — A Visual Comparison

Reviewed & accurate
AI Summary

What You'll Learn

Beginner

  • How virtual machines work under the hood
  • How containers work under the hood
  • The key architectural differences
  • When to use containers vs VMs (or both)
Virtual Machine App A App B Bins/Libs Bins/Libs Guest OS (1-2 GB each!) Hypervisor Host OS Infrastructure Container App A Bins/Libs App B Bins/Libs Docker Engine Host OS Infrastructure Heavy: GBs per VM Full OS per instance Light: MBs per container Shares host kernel
Containers vs VMs — containers skip the Guest OS, sharing the host kernel directly

How Virtual Machines Work

A virtual machine (VM) is a full computer simulated in software. It includes:

  • A full guest operating system (Windows, Linux, etc.) — 1-4 GB minimum.
  • Binaries and libraries for that OS.
  • Your application on top.

A hypervisor (VMware, VirtualBox, Hyper-V, KVM) creates and runs VMs. It sits between the host hardware and the guest operating systems, allocating CPU, RAM, and disk to each VM.

VM architecture (bottom to top)

Infrastructure — the physical server.
Host OS — the operating system on the physical server.
Hypervisor — software that creates and manages VMs (VMware, Hyper-V, KVM).
Guest OS — each VM runs a full operating system. This is the big cost.
Binaries/Libraries — the guest OS's tools and libraries.
App A / App B — your applications, each in its own VM.

How Containers Work

A container is an isolated process running on the host OS. It does not include a guest operating system. Instead, it uses the host's kernel directly, with isolation provided by Linux features called namespaces and cgroups (covered in lesson 03).

Container architecture (bottom to top)

Infrastructure — the physical server.
Host OS — the operating system (shared by all containers).
Docker Engine — software that creates and manages containers. No hypervisor!
Bins/Libraries + App — each container has its own libs and app, but shares the kernel.

The Key Difference

The crucial insight
VMs virtualize hardware — each VM gets a virtual CPU, RAM, and disk, plus a full OS. Containers virtualize the operating system — they share the host's kernel and only carry what's unique to the app.

Detailed Comparison

AspectVirtual MachineContainer
VirtualizesHardwareOperating System
Guest OSYes (full OS per VM)No (shares host kernel)
Size1-4 GB+ each10-200 MB each
Startup30-120 seconds0.1-2 seconds
Memory overheadEach VM needs RAM for its OSOnly the app uses RAM
IsolationStrong (separate kernel)Good (shared kernel, isolated processes)
Security boundaryHardware-levelProcess-level
Density2-4 VMs per host10s-100s per host
Cross-OSRun Linux on Windows, Windows on LinuxLinux containers need Linux kernel

When to Use Virtual Machines

Choose VMs when
  • You need to run a different operating system (e.g., Windows app on a Linux host).
  • You need strong security isolation — separate kernels mean a kernel exploit in one VM doesn't affect others.
  • You're running legacy applications that expect full OS control.
  • You need hardware-level features like custom kernel modules or specific device drivers.

When to Use Containers

Choose containers when
  • You're deploying microservices or web applications.
  • You want fast startup for scaling and CI/CD.
  • You need high density — many instances on one host.
  • You want consistent environments across dev, staging, and production.
  • You're building cloud-native applications.

The Best of Both Worlds

In production, most teams use both:

  1. Cloud provider runs your containers on VMs (or bare metal) for security isolation.
  2. Each VM runs a container runtime (Docker, containerd).
  3. Containers provide the application density and fast startup.

This is exactly how Kubernetes works — it schedules containers across multiple VMs (called "nodes").

Docker CLI docker run... docker build... REST API dockerd (Daemon) Containers Images Networks / Volumes pull/push Registry Docker Hub Private Reg Docker Host You type here Work happens here Images stored here
Docker Architecture — Client sends commands, Daemon does the work, Registry stores images

Performance Comparison

MetricVMContainer
CPU overhead2-5% (hypervisor translation)Near-zero (native speed)
Memory overhead512 MB - 2 GB per VM (for OS)~0 (only app memory)
Disk usage10-40 GB per VM50-500 MB per container
Network latencyHigher (virtual NIC)Lower (host network)
I/O performance90-95% of native97-100% of native

Common Mistakes

Avoid these
  • Thinking containers replace VMs. They complement each other. Most production setups use both.
  • Assuming containers are as isolated as VMs. They're not. A kernel exploit affects all containers on the host.
  • Running different OSes in containers. Linux containers need a Linux kernel. Windows containers need Windows. You cannot run a Windows app in a Linux container.
  • Ignoring VM-level isolation for sensitive workloads. Multi-tenant services often use VMs between customers, containers within each customer.

Practical Exercise (5 minutes)

Run docker run -it ubuntu bash — instant Ubuntu container
Inside the container, run uname -a — see the kernel version (it's the host's kernel!)
Run cat /etc/os-release — see the Ubuntu version
Type exit — container stops instantly
Compare: if you started a Ubuntu VM, it would take 30+ seconds. The container took less than 1 second.

Mini Challenge

List three applications or services you use daily. For each, decide: would it be better as a container or a VM? Why? (Hint: web apps, APIs, and microservices are usually containers. Legacy enterprise software with OS-specific dependencies might need VMs.)

Key Takeaways

  • VMs virtualize hardware; containers virtualize the OS.
  • VMs include a full guest OS (GBs); containers share the host kernel (MBs).
  • Containers start in seconds; VMs take minutes.
  • VMs offer stronger isolation; containers offer higher density.
  • Most production setups use both — VMs for isolation, containers for density.
Previously: Lesson 01 introduced Docker and the shipping container analogy.
Today: You did a deep visual comparison of containers vs VMs — architecture, performance, and use cases.
Next: Lesson 03 goes under the hood: namespaces, cgroups, and how Docker actually isolates processes.

FAQ

Can I run Docker inside a VM?

Yes, and this is extremely common. Cloud providers (AWS, Azure, GCP) run your containers inside VMs they manage. You get VM-level isolation between customers and container-level density within your VM. This is called "nested virtualization" or simply "containers on VMs".

Are Windows containers different from Linux containers?

Yes. Windows containers run on Windows hosts only, using Windows isolation primitives. Linux containers run on Linux hosts (or inside a Linux VM on macOS/Windows via Docker Desktop). You cannot run a Windows container on a Linux host — the kernel must match.

Test Your Knowledge
How did you find this?

Comments

Join the discussion! Sign in with your Google or Blogger account, or comment as Anonymous - no account needed. For quick questions, also reach me on Telegram @cytestch.

Comments