🎓 A1Learner
🔍
0 of 50 lessons completed

Containers vs Virtual Machines

A container shares the host operating system, and a virtual machine runs a whole guest operating system. That one difference shapes everything else.

See how small an image is

This command shows the size of the hello-world image.

docker images hello-world --format "{{.Repository}}:{{.Tag}} {{.Size}}"

Compare with a full system image

An nginx image carries a whole web server, so it is far larger.

docker images nginx --format "{{.Repository}}:{{.Tag}} {{.Size}}"

Example

docker images hello-world --format "{{.Repository}}:{{.Tag}} {{.Size}}"

Key points

  • Virtual machine: a full computer in software, with its own guest operating system.
  • Container: a running process that shares the host kernel.
  • A virtual machine boots in minutes. A container starts in seconds.
  • A virtual machine needs gigabytes of disk for its guest system.
  • A container image is often tens of megabytes, because it holds no operating system.
  • Use containers for light, fast, repeatable apps. Use virtual machines to run a different operating system.

In this example, Docker lists images with their sizes. The hello-world image is only 21.8kB. A virtual machine disk for the same job would measure in gigabytes. The tiny size is the payoff of sharing the host operating system.

Note

Containers share the host kernel, so a container cannot run a different kernel from its host.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Rust Playground

Exercise

Why does a container start faster than a virtual machine?