Virtual machines are created on top of a hypervisor, which runs on top of the host machine’s operating system. With hypervisors, we emulate hardware like CPU, disk, network, memory, etc., and install guest machines on it. We can create multiple guest machines with different operating systems on a hypervisor. For example, we can take a Linux machine running on bare metal, and after setting up the hypervisor, we can create multiple guest machines with Linux and Windows operating systems. Examples of a hypervisor include:
We can find support for hardware virtualization in all recent CPUs, as it is important to share the host system’s processor resources with multiple guest operating systems in a safe and efficient way. Most of the recent CPUs will also support nested virtualization, which enables us to have a VM inside a VM.
A virtual machine runs on top of a hypervisor, which emulates different hardware, like CPU, memory, etc., so that a guest OS can be installed on top of them. Different kinds of guest OSes can run on top of one hypervisor. Between an application running inside a guest OS and in the outside world, there are multiple layers: the guest OS, the hypervisor, and the host OS.
On the other hand, containers run directly as a process on top of the Host OS. There is no indirection as we see in VMs, which helps containers to get near-native performance. Also, as the containers have very little footprint, we can pack more containers than VMs on the same physical machine. As containers run on the host OS, we need to make sure containers are compatible with the host OS.
The following diagram illustrates the difference between a virtual machine and a container: