Comparing VM's and Containers

Both technologies are concerned with virtualization, both need a piece of software to run, both have images, both are concerned with operating systems. So what's the difference between the two?

According to Wikipedia "virtualization" is "the act of creating a virtual (rather than actual) version of something". Very vague indeed, let's explore further.

VM

A virtual machine (VM) is hardware virtualization, it is a virtual computer, complete with ram, hard disk, CPU resource allocation. A VM can be just this virtual computer, however an operating system is usually installed making it usable. Once a VM has been created it can be exported into VM image. The VM obviously can have any other application installed as you can like any other computer.

In an execution environment a VM is a "guest machine", run via a hypervisor/virtual machine monitor (VMM) on a "host machine". There are types of hypervisors, bare-metal (type-1) and hosted (type-2).

fig 1. Different types of hypervisors





















fig 2. Laptop (host machine) running Mac OS X (host OS), using VirtualBox (type-2 hypervisor) to run Ubuntu VM (guest OS)


















Container

A container, on the other hand, is operation-system-level virtualization also known as containerization. The rest of this section will be based on the most popular container technology Docker.

Docker works by exploiting features of the Linux kernel (cgroups and namespaces) isolating each container while sharing the single kernel from the host Linux OS.


fig 3. Container technology compared with VM

















fig 4. Laptop (host machine) running Mac OS X (host OS), running Docker(container engine), running mongo (container image)












Docker images such as this mongo docker image are created in layers. The root layer is always an "operating system" layer. This root OS layer is a little misleading as it does not, in fact, contain an entire OS. Instead, it contains the components of a Linux distribution without the Linux kernel. This is why you can run multiple Docker containers of different Linux distributions e.g. Ubuntu based, Debian based, Alpine based on the same host machine. as they will share the same host Linux kernel.

How does Docker have compatibility on Windows and Mac OS X (an OS based on Unix) when neither has a Linux kernel for the containers to use? Essentially a hypervisor is used, see Microsoft Hyper-V and macOS Hypervisor framework. Thus from the perspective of the container images, there is no difference between this environment and a native Linux host environment.

Comparison

Since running a container does not start an OS, this is why they start much faster than a VM. This is also why container image sizes are much smaller.  And also this is why many more containers can run on the same host machine when compared with a VM. A VM in contrast has better isolation compared with containers.

Resources

- http://www.linuxandubuntu.com/home/virtualbox-vs-container

- https://stackoverflow.com/questions/16047306/how-is-docker-different-from-a-virtual-machine

Comments

Popular posts from this blog

Comparing REST and GraphQL

Comparing the execution model of Spring Boot, Akka and NodeJs