What is the difference between a Docker container and a virtual machine?
Fast recall
Quick revision
Core answer
Interview-ready guide
Interview answer
A virtual machine virtualizes hardware. Each VM boots a guest operating system with its own kernel, then runs the application. A container packages an application and filesystem layers but runs isolated processes using the host kernel—or, on Docker Desktop, the Linux VM's kernel.
For example, three Java containers can share one Linux kernel while each has its own process, network, and filesystem view. Three Linux VMs each have a guest kernel and OS services. Containers therefore often start faster and allow denser packing, while VMs can provide a stronger isolation boundary and support different guest operating systems.
I avoid fixed claims such as 'containers always use megabytes and VMs always need gigabytes.' A large Java heap can dominate either design, and lightweight VMs can boot quickly. The choice depends on isolation, kernel needs, operations, workload density, and platform—not only speed.
What is the difference between a Docker container and a virtual machine
Direct Answer
A virtual machine presents virtual hardware and runs its own guest operating-system kernel. A container isolates processes while sharing the host kernel (or the kernel of the VM that hosts the container engine). Containers often start faster and use less overhead, but exact startup time and memory vary; VMs provide a stronger kernel boundary and can run different guest operating systems.
Code Example
# Measure the real container instead of memorizing a size rule
docker run -d --name order-api order-service:1.0
docker stats --no-stream order-api
docker inspect --format '{{.State.StartedAt}}' order-api
# Inspect image storage; JVM heap/application data can dominate usage
docker image ls order-service:1.0
Compare an equivalent VM and container workload on the target platform; do not use universal seconds or MB/GB figures.
Common Mistakes & Pitfalls
- Memorizing size and boot-time numbers: measure the actual image, JVM, VM, and platform.
- Saying a container has its own kernel: it shares the hosting kernel.
- Saying VM escape is impossible: both boundaries need patching and hardening.
- Treating containers and VMs as exclusive choices: they commonly form two layers of one deployment.
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.