Difference between virtual machines and containers


Virtualization and containerization might seek to achieve the same goals but they are different concepts.

The virtual machine loads an OS on its own it can be any type of OS that you would like. The virtual machine has its own hard drive, network interfaces etc. that are then mapped to the physical hardware of your machine. So you can easily run a Windows or macOS machine on you Ubuntu if you'd like to. Virtualization however is different from emulation, so you're still limited to the hardware that you have available.

Containerization is the process of isolating the application dependencies into a container. It uses kernel functionalities (like control groups and namespaces) that allow for resource isolation. Since you're not running a full guest OS you won't be able to run a Windows container on a Linux machine and vice versa. Docker didn't invent the concept of containers, but is the most popular solution at the moment and when people are talking about containers they usually refer to this solution.

Software required

In order to run a virtual machine on your computer you need a hypervisor. As per Wikipedia a hypervisor is computer software, firmware or hardware that creates and runs virtual machines. There are two different types of hypervisors: type-1 or native ones run directly on the computer hardware while type-2 or hosted ones run on the host OS as a process. Examples of hypervisors a Microsoft Hyper-V, VirtualBox or VMware Player.

The docker containers on the other hand are executed with the Docker engine that runs on the host OS.

Resource isolation

The virtual machines  operate within a sandbox, meaning that they are completely isolated and self-contained. The containers share the operating system while virtual machines run an OS on their own. Resource isolation is one of the advantages that virtual machines have over containers.

Resources consumption

Most companies choose Docker over VMs because of much smaller resource footprint. Virtual machines might also need to have the resources assigned to them permanently while they're operating, containers allow for more flexibility and optimization.

There is also less redundancy when using containers - you don't have to install the full guest operating system, just components that you need for your application.

Each VM runs not just a full copy of an operating system, but a virtual copy of all the hardware that the operating system needs to run. The guest OS has its own memory management and virtual device drivers.  As a result containers are smaller that VMs and have superior startup times than virtual machines. In many cases you might double the number of applications run and the speed when using containers. However, this might not be true in all situations. Current technology of virtualization reduced the overhead of CPU and memory usage and in some configuration a virtual machine might run faster than a Docker container [1].

The biggest advantage of containers is that you can create a lightweight, portable and above all consistent operating environment when you're developing, testing and deploying applications.