Skip to content
$ lynuxpro
← Docker

LESSON 1 OF 18 · BEGINNER · 14 MIN

Containers vs virtual machines

Free previewLesson 1 of the 2 free lessons in this Pro course.Unlock all 18

HOW IT WORKSVirtual machines vs containers

VIRTUAL MACHINESCONTAINERSApp A+ librariesApp B+ librariesGuest OSfull kernel, GBsGuest OSfull kernel, GBsHypervisoremulates hardwareHardwareCPU, RAM, diskContainer Aapp + libs, MBsContainer Bapp + libs, MBsShared kernelLinux host OSDocker Enginecontainerd + runcHardwareCPU, RAM, disk
1/5

With virtual machines, every app ships with its own complete guest operating system, which costs gigabytes of disk and memory each.

Before containers, teams shipped software by handing operations a set of installation instructions, or a whole virtual machine. Both were slow and fragile: "it works on my machine" was a daily problem. Containers fixed this by packaging an application together with everything it needs, in a format that runs the same on a laptop, a CI runner and a production server. This lesson explains what a container actually is and how it differs from a virtual machine.

In simple words

  • Container: a packaged app with everything it needs (code, libraries, settings) that runs as an isolated process on a shared Linux kernel. It starts in seconds.
  • Virtual machine (VM): a whole pretend computer with its own operating system, running on a hypervisor. Heavier and slower to start than a container.
  • Hypervisor: the software that creates and runs virtual machines, such as VMware, KVM or the cloud's own.
  • Isolation: keeping one app from seeing or affecting another, even though they share the same machine.

The problem containers solve

Imagine a Java service that needs Java 21, a specific version of OpenSSL and a config file in /etc/app. On your laptop it works. On the test server, Java 17 is installed, so it fails. On production, someone upgraded OpenSSL last week, so it fails differently.

A container image bundles the app and its runtime, libraries and files into one artifact. Wherever the image runs, the app sees exactly the same environment.

Without containersWith containers
Install runtimes on every server by handThe runtime is inside the image
Different library versions everywhereIdentical files everywhere
Deploy = run a script and hopeDeploy = run the image
Rollback = reinstall the old versionRollback = run the previous image tag

Virtual machines in one minute

A virtual machine (VM) emulates a complete computer. A hypervisor (VMware ESXi, KVM, Hyper-V, or the one behind every EC2 instance) divides physical hardware into several virtual machines, and each one boots its own full operating system.

  • Each VM has its own kernel, so you can run Windows and Linux side by side.
  • Isolation is strong: a bug in one guest kernel does not affect another.
  • But every VM carries a full OS: gigabytes of disk, hundreds of MB of RAM before your app even starts, and boot times in minutes.

Containers in one minute

A container is an isolated process running on the host's own Linux kernel. There is no guest OS and no emulated hardware. The kernel uses two features to make the process feel like it has its own machine:

Kernel featureWhat it isolates or limitsExample
NamespacesWhat the process can see: its own process list, network, hostname, filesInside the container, ps shows only the app
cgroupsWhat the process can use: CPU, memory, number of processes--memory 512m caps the container's RAM

On top of that, the container's files come from an image: a read-only bundle of the app and its dependencies.

Side by side

Virtual machineContainer
What is virtualisedHardwareThe operating system view
KernelIts own guest kernelShares the host kernel
Typical size1–20 GB5–500 MB
Start time30 seconds to minutesMilliseconds to a few seconds
DensityTens per serverHundreds per server
IsolationStrongGood, but a shared kernel means kernel bugs matter
Can run a different OS?Yes (Windows on Linux host)Only Linux containers on a Linux kernel*

*Docker Desktop on macOS and Windows quietly runs a small Linux VM to host Linux containers. That is why containers "work" there too.

They are not competitors

In practice you use both together. Your containers run inside VMs: an EKS worker node is an EC2 virtual machine running dozens of containers. The VM gives strong isolation between customers and teams; containers give fast, dense, repeatable app packaging inside it.

Your first container

In the practice terminal, run:

docker run --rm hello-world
SHELLbashutf-8 · 1L
Hello from Docker!This message shows that your installation appears to be working correctly.
OUTPUTterminalutf-8 · 2L
  • docker run creates and starts a container from an image.
  • --rm deletes the container automatically when it exits.
  • hello-world is a tiny image whose only job is to print that message.

Now start something longer-lived and look at it:

docker run -d --name web -p 8080:80 nginxdocker ps
SHELLbashutf-8 · 2L
CONTAINER ID   IMAGE    STATUS         PORTS                  NAMES3f9a1c2b7d4e   nginx    Up 2 seconds   0.0.0.0:8080->80/tcp   web
OUTPUTterminalutf-8 · 2L

A full web server started in about a second. A VM with nginx would still be booting.

Proving it is just a process

On a Linux host (a live lab), the container's process is visible from the host like any other program:

$ ps -ef | grep "nginx: master"root   48213  48190  0 10:02 ?  00:00:00 nginx: master process nginx -g daemon off;
OUTPUTterminalutf-8 · 2L

Inside the container, the same process thinks it is PID 1 and sees nothing else. Same process, two different views: that is a namespace at work. You will take this apart in the Expert section of this course.

Real-world scenario

A team runs 12 microservices. With one VM per service they paid for 12 VMs that were 90% idle, deploys took 20 minutes, and environments drifted apart. After containerising:

  • All 12 services run as containers on 3 larger VMs (an EKS node group).
  • A deploy pulls a new image and swaps containers in under a minute.
  • The image tested in staging is byte-for-byte the image in production.

Common mistakes

  • Treating a container like a VM: installing SSH, running many services in one container, or logging in to patch it. Containers should be replaced, not repaired.
  • Assuming containers are a security boundary as strong as VMs. They share a kernel; harden them (covered in the security lesson) and use VMs or sandboxed runtimes between untrusted tenants.
  • Storing important data inside the container. It disappears with the container unless you use volumes.

Practice

  1. Run hello-world with --rm, then run docker ps -a and confirm no container was left behind.
  2. Start nginx with docker run -d --name web -p 8080:80 nginx and list running containers.
  3. In one sentence each, explain what namespaces and cgroups do.
  4. Explain why an EKS worker node is both a VM and a container host.

Cheat sheet

CommandWhat it does
docker run --rm IMAGERun a container and delete it when it exits
docker run -d --name N IMAGERun in the background with a name
docker ps / docker ps -aRunning / all containers
docker versionClient and server versions

Key takeaways

  • A VM virtualises hardware and runs its own kernel; a container is an isolated process on a shared kernel.
  • Namespaces control what a container can see; cgroups control what it can use.
  • Containers are small, fast and repeatable, and in production they usually run inside VMs.