Virtual Machines vs Containers
Tags: cloud, beginners, devops, docker
Published: 2022-06-21 07:27:11


![Screen-Shot-2018-03-20-at-9.24.09-AM_tcm19-56643.jpg](https://cdn.hashnode.com/res/hashnode/image/upload/v1642230264591/rdPA8CT_s.jpeg)
## Overview

Virtual machines and Containers are two ways of deploying multiple, isolated services on a single platform. They have distinct and unique characteristics, but they are also similar for the fact that they both improve IT efficiency, provide application portability and enhance DevOps and the software development lifecycle.

## How virtualization works

Virtualization is an advanced software engineering concept that allows creating software-based virtual representations of servers, storage and networks. It is a technique in which a software is used to simulate hardware functionality.

The software used is called a Hypervisor - a software that creates and manages virtual machines, mediates communication between host and vms, and allocate resources to each vm. When a hypervisor is used on physical computer or server (bare metal server) in a data center, it allows the physical computer to separate its operating system and applications from its hardware. Then it can divide itself into several independent "virtual machines".

## What are Virtual Machines?


![download.jpg](https://cdn.hashnode.com/res/hashnode/image/upload/v1642244016030/dA3mJfFxq.jpeg)
VMs are heavy software packages that provide complete emulation(the process of imitating a hardware/software program/platform on another program or platform) of low level hardware devices like CPU, Disk and Networking devices. This makes it possible to run programs on systems not designed for them.

> 
Simply put, a virtual machine is an emulation of a physical computer. 

Virtual machines are known as Hardware Virtualization because it happens at the hardware level. On top of the hardware is the Hypervisor. The hypervisor is responsible for creating virtualized instances on each of the components that make up our machines (processors, ram, storage). Multiple operating systems run on one peice of hardware (in partitions) and the host operating system is the hypervisor. The hypervisor is the key to enable virtualization. It manages the sharing of physical resources into virtual machines. Each virtual machine runs its own guest operating system. They are less agile and have low portability than containers.


## What are Containers?

![download.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1642230158331/jdiNZGxCL.png)
Containers are lightweight software packages. They are executable units of software in which application code is packaged, along with its libraries and dependencies, in common ways so that it can be run anywhere, whether it be on desktop, traditional IT or the cloud. The operating system also runs on the the hardware, but instead of using a hypervisor as a host operating system, we use container engine. This container engine exposes part of the host operating system into the partitions. The partitions only contain the binaries and libraries, the applications that we need, they don't contain an entire operating system because they can share the kernel of the underlying operating system. Having such shared resources, we loose seperation and independence but we gain a ton of efficiency and that's what makes containers really powerful
These two technologies are different ways of achieving virtualization.

## VMs vs Containers


![SWTM-2060_Diagram_Containers_VirtualMachines_v03.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1642230369219/KQ57DpRpv.png)

### Startup Time
VMs have an entire copy of an operating system that takes time to boot. Containers run off a kernel that's already running, boot up in milliseconds that makes them rapid to deploy which is good for cloud autoscaling.

### Disk Space
Having an entire copy of an operating system running in a virtual machine, its going to consume space. The more VMs, the more duplicate data which consumes extra storage. In they case of containers, they share. Using a container image, they fire up with just their applications. They are far more efficient as far as the storage they consume. 

### Portability
Virtual machines can move between hardware assuming the hardware is running the same hypervisor. Containers have services that are compatible with docker containers. This means you can write a docker container and run it on a laptop, you can run in in a server, the AWS cloud or Microsoft Azure. They are very portable, easy to move around. This doesn't happen with virtual machines.

### Efficiency
Virtual machines have an entire copy of operating systems so they consume more Ram, more CPU, more disk space. Containers are far more efficient. It trades isolation in exchange for performance and efficiency gains.

### Operating system / Kernel
With virtual machines, you have dedicated operating systems and kernels per virtual machine. If one VM crashes, it doesn't affect the others. With container platforms, they use a shared kernel. It makes them more efficient and faster, but that kernel crashes, all the containers would go down. Fortunately, they are so small and can be rapidly redeployed somewhere else.  


## Which option is better for you?

If you have specific hardware requirements for your project, or you are developing on one hardware platform and need to target another like Windows vs MacOS, you will need to use a VM. Most other 'software only' requirements can be met by using containers. Both containers and VMs have pros and cons and therefore affect the decision you make. Ultimately, your choice depends largely on two things:

1. Do you need a lightweight instance for easy migration between bare-metal systems?

2. Do you want a semi-permanent allocation solution for your IT resources?


### You can consider containers if you want to:

- Implement cloud-native applications.

- Package microservices.

- Adhere to DevOps and continuous integration/continuous deployment (CI/CD) practices.

- Move scalable applications across different IT environments that share the same OS.


### You can consider VMs if you want to:

- Host legacy and monolithic applications.

- Provision IT resources such as servers, storage and networking.

- Isolate risky development environments.


## How can you use Containers and VMs together?

You might be wondering why someone would want to use containers and VMs together. Well,
containers share the host’s OS kernel, including binaries and libraries. Running Linux containers across different distributions is not an issue because most Linux distributions are built on top of the same kernel. It is entirely possible to use Containers and VM in unison although the practical use-cases may be limited. A VM can be created that emulates a unique hardware configuration. An operating system can then be installed within this VM's hardware. Once the VM is functional and boots the operating system, a container runtime can be installed on the operating system. At this point we have a functional computational system with emulated hardware that we can install Containers on.

For example, you can run Ubuntu containers on CentOS-based hosts efficiently. However, the kernel sharing fact means that you cannot run Windows containers on Linux hosts and vice versa. To run these containers, you need to create a VM on the respective host. This is possible because a virtual machine utilizes its own OS, allowing the operating system to support the container engine. 

One practical use for this configuration is experimentation for system on chip deployments. Popular system on chip computational devices like The RaspberryPi, or BeagleBone development boards can be emulated as a VM, to experiment with running containers on them before testing on the actual hardware.

But the majority of the time, your needs will likely be met by one of the two. The key to deciding between containers or VMs for your virtualization needs is understanding your resource needs and the trade-offs you’re willing to make.

Thank you for reading!