Virtual Machines and Containers: A quick comparison

Mohnish Srivats
Developer Community SASTRA
5 min readApr 19, 2021

“Cloud computing” has evolved a lot over the past two decades and has now become a game-changer everywhere in the tech industry. In simple terms,

Cloud is an on-demand(pay for what you use) availability of computing resources that are accessed through the internet without direct management by the user.

The above definition gives an abstract idea of what cloud computing is. Digging a little deeper, we find that there are a large variety of services provided by cloud vendors that cover all types of business requirements. The most common and important services are Compute, Storage, Network, and Security. (included with a lot of micro-services)

Source:https://networkencyclopedia.com/

The soul of this blog, Virtual Machines(often referred to as VM), and Containers come under the compute services of cloud computing. Both services look similar and hence it is important to understand the differences between them to choose the right service for the right purpose. Let’s break it down and get started!!

What is a Virtual Machine(VM)??

VM’s are complete emulation of a physical computer. To be precise you don’t have to buy any physical hardware and maintain it, rather the cloud vendors do it for you in their data centers. But the underlying infrastructure and the host OS(Operating system) remain the same. Each VM includes an OS and hardware that appears to the user just like a physical computer.

What is a Container??

It is cool that you don't have to spend upfront on your infra. But you have to boot and initialize your VM each time. Instead, all the code and its dependencies are packed together in form of a container which is like an image for the OS (a single file containing all functionalities of OS). Then, a standard runtime environment is used for the execution of the application.

Comparison based on Characteristics

To get a clearer picture of these services it is necessary to know their differences, as people often think VMs and Containers are the same. Some factors that can be considered are:

1. Virtualization level:

  • VMs can be treated as a “template for your physical hardware”. They are built above the hardware through an abstract layer called the hypervisor (a medium to facilitate multiple guests for one host) above which VM runs with the presence of a guest OS apart from the host OS.
Layers of Virtual Machine (Source-Microsoft docs)
  • Container instances are “templates of the Operating system”. Here the complete hardware is not virtualized, rather the container engine holds all the code for the execution above the kernel and runs instances.
Layers of Container (Source-Microsoft docs)

2. Level of Isolation:

  • When you use a VM, it looks like a separate computer is allocated for you which means that everything from memory, OS, and disks are dedicated during deployment. So if you deploy a VM the “isolation is at machine level” and above the server, you can run several VM which are independent.
  • In a container, the “isolation is at process level” which means that you can run several container instances under one OS. It’s like having many applications run at a time on your laptop parallelly.

3. Execution:

  • As mentioned earlier, with the use of a VM you can have control over your underlying infrastructure. But at crucial times it might be difficult as it takes a lot of time for the VM to start, just like how your system might take a lot of time to boot up.
  • The deployment in a container happens in milliseconds because there is no OS to boot and initialize all the time. With the help of container orchestration services like Kubernetes, you can deploy your applications within seconds even during an emergency.

4. Flexibility:

  • In a VM, flexibility is a bit costly in terms of both resources and money. Let’s say we have a VM which can handle 100 users at a time. If the users count increases by 200 we can deploy a new VM. But if it drops down again the new one gets wasted. So we have to decide it based on the needs.
  • On the other hand, when you have containerized applications there is no need to wait for the system to load from scratch and boot up. All you need is an OS that supports containers. With Kubernetes in hand, there is also an option to auto-scale resources (automatic scaling).

Choose the right one!!

Based on the differences above it may look like containers are better to use. But “cloud” is a solution for business organizations that have different requirements at different phases. So it completely depends on the organizational needs. Here’s an analogy to iterate the same.

A person with a family who has members of different ages might look for an independent house to live in. Whereas, bachelors might look for an apartment that allows sharing of rooms among themselves. So here comes the point!! One can’t generalize which solution to be used.

Some big MNC’s that gives more importance to the underlying resources might seek a VM. On the other hand, a company with lesser revenue or a startup might deploy containers, as the end product will be their ultimate moto with lesser costs and resources.

Where are Virtual Machines used often??

  • When an organization “extends its data center from an on-premise to cloud-based solution it can opt for VMs. For example, an application might need to handle fluctuations in demand. Administrators can easily delete the VMs when they no longer need them.
  • In SDLC (software development life cycle) the phase of testing and development is also suitable as it’s an easy way to create different OS and implement new configurations in the application.
  • If a primary data center (on-premise) becomes un-operational, you can create VMs to handle some critical workloads and shut them down if it restores normalcy. Thus it helps in disaster recovery.

When to opt for Containers??

  • Containers support increased portability and can be used when you have to switch among several processes.
  • They are also often used when you adopt a micro-service architecture for your solution. This architecture is employed when you break your solution into smaller independent pieces. Example: Implementing a multi-schema architecture for your website which has a front end and a back end.
  • Containers act as an intermediate ground between VMs and serverless options, thus giving you more control and taking less time to execute.

Conclusion

So to conclude, if you are in a state to run multiple instances of your application containers will be a better choice. At the same time, if you want to run several applications then it’s better to go for VMs.

While cloud computing is evolving rapidly and you are probably using it already either for business or for personal means. Thus, it is really important to know your needs clearly (either business or development) and start looking for migration in an organic way.

Thanks for being here!!

--

--