Skip to content
Back to blog

Containers vs VMs

devopscontainerscloud

While studying containers for the first time, a question (which is also a popular interview question) came into my mind: “If containers also provide an isolated environment, then what is the difference between containers and Virtual Machines?” So, here we go.

Let’s understand both of them individually then conclude with their differences.

Virtual Machines

Cloud providers like AWS have multiple ultra high performance computers, each high performance computer is known as a server. Now if we want to deploy our applications to these servers, it would be foolish to rent the entire server. Renting an entire giant machine to a single customer who will not be going to utilise even 5–10% of its compute makes no sense.

Here comes the concept of virtual machines. Providers allow users to rent a part of the servers based on their needs. They use a hypervisor (a software layer that lets a single physical computer run multiple VMs at the same time) to slice each physical server into hundreds of virtual machines.

So basically the cloud server you actually rent is a virtual computer inside a physical one. Each virtual machine has its own virtual hardware configurations and guest operating system. Now let’s say you want three VMs for your application — you must install 3 separate copies of OS. This gave rise to the spin-up problem.

Spin-up Problem

When we restart a VM, it must go through the exact lengthy boot process as a physical laptop turning on from a dead stop. This process can take up to minutes which worsens the response time and user experience.

Containers

We see containers from 2 different perspectives:

  1. What does it contain?
  2. How does it run?

What does it contain?

A container packages all the application files, dependencies, and required libraries. It packages everything your application needs, so that it can run consistently in different environments.

How does it run?

It runs in an isolated environment. On Linux this isolation is provided using namespaces (they isolate what a container can see — namespaces can give a container its own PID, filesystem, etc.) and cgroups (control groups restrict how much system resources a container can use).

The Key Difference

This was the point when my mind asked me the difference between VMs and Containers. So here it is:

Containers do not have their own operating system. Instead they share the host operating system’s kernel. On the other hand, VMs have their own guest OS, including their own kernel.

That’s what makes containers so much faster to start, there’s no full OS boot sequence. Just process isolation on top of a shared kernel.