A Beginner’s Guide to Containers and Virtual Machines

Last time, we took a dive into the world of virtual machines (see “Cloud Basics: What is a Virtual Machine?“) and saw how this technology powers the world of cloud computing. Today, we’ll take a look at containers, a technology developed after virtual machines to solve some of their drawbacks.

What is a Shipping Container?

Before we delve into containers in the technology world, it would be useful to briefly examine shipping containers since cloud containers are named after and based on them in terms of modularity and operation.

In short, a shipping container is a reusable steel box used for storage and shipment. Its portable design allows it to be easily transported on a large cargo ship, attached to a train, or a truck without any changes to its design or structure.

Their design allows for efficient utilization of space, enabling many containers to be stacked on top of each other, thereby allowing a large volume of goods to be transported simultaneously.

Shipping containers

What is a Container?

Just like shipping containers, a container in the world of technology is used for packaging and shipping. However, instead of transporting goods from point A to point B, it transports an application and all its dependencies, such as code, runtime, tools, and libraries to a given environment, like test, QA or production.

Containers are lightweight, quick to deploy, and secure since they are isolated like virtual machines, meaning one container cannot directly affect another. One of the main benefits of containerization, and the reason for its existence, is its ability to work on any type of infrastructure, as long as the required container engine is installed on the host. This eliminates the all-too-familiar “but… it works on my machine” problem.

Virtual Machines vs Containers

VMs and containers are both used for the deployment and hosting of applications and services, but they differ significantly in how they work and scale. Let’s compare both technologies.

A virtual machine (VM) is a software-based, or “virtual,” version of a computer, equipped with its own dedicated operating system (OS), CPU, memory, and storage, assigned by the physical machine within which it resides, known as the host. The host could be your personal computer or a server in a cloud provider’s data center.

Since every VM has its own OS, this leads to higher resource consumption and slower boot times, making them inefficient for rapid scaling. It could take at least a few minutes for a single VM to be provisioned and ready to handle requests.

IMG1 – Virtual Machines (VMs) infrastructure

Unlike VMs, which abstract (or virtualize) the hardware of the host, containers abstract the operating system (OS), making them much more portable, lightweight, and efficient.

While every VM must have its own OS, many containers can reside on the same host, sharing the same OS and underlying resources. This allows for scaling out (adding more containers) within seconds, thus enabling the system to handle traffic spikes without concern about resource availability, as long as sufficient resources exist on the host machine.

IMG2 – Containers and containerization infrastructure

Just as VMs have a hypervisor responsible for the virtualization, containerized systems have a container engine that manages the environment. Every container is designed to be isolated from others, ensuring it operates as if it were the only entity in the system.

It’s also important to note that containers are constrained to the operating system of the host, so a Linux container cannot run on a Windows host, and vice versa.

VMs & Containers Together

Even though containers might seem like a better option than virtual machines, in reality, they often work together, combining their strengths to deliver the most optimal solutions.

Cloud providers typically purchase large, powerful servers and virtualize them to appear as if they’ve magically created hundreds or even thousands of separate machines from just one.

They may rent these machines to clients as is, through services like Azure VM or Azure App Service, which are based on VMs, or they could offer services like Azure Container Instances (ACI)Azure Container Apps, or Azure Kubernetes Service (AKS). These services are hosted on VMs, but containers are used on top of them for better resource utilization, scalability, and resiliency.

Here’s how a system like that looks like:

Containers and Virtual Machines working together

You have the infrastructure (the server), on top of that you have the host OS and a hypervisor. The hypervisor virtualizes the host resources and provisions virtual machines with dedicated resources – CPU, RAM, storage, etc.

On top of the VMs, you have a container engine responsible for the containerization process. The containers can now share the resources of the VM on which they are provisioned.

And that’s how you get the best of both technologies – VMs for virtualization of the underlying hardware and containers for fast deployment, scalability, and resiliency.

This is a post I posted on my other blog too – ChooseAzure.com, so go check it out 🙂