Docker Containers vs. Virtual Machines
Tags: vm, docker, virtualmachine
Published: 2024-05-03 15:59:20

In a previous article I talked about how to install a NextJS app on a docker container. Now, I feel the need to write an article about the differences between docker containers and virtual machines.

As a declaration of interests, I only run containers in my production apps. I believe that having a dedicated virtual machine for just running an app is overkill. Also, I don't like to have all the apps installed in a virtual machine where I can make a mistake and mess up the environments of the different apps.

For that reason my current setup for production apps is two vms, each of them running docker with a few containers. When I need to update a container I just do a `git pull` followed by a `docker stop <container name>` and `docker build` and `docker start <container name>`. So far it has worked without any hiccups.

With that out of the way let's get into the article.

## What is a Virtual Machine?

A virtual machine is an emulation of a computer system that provides the functionality of a physical computer. It operates based on a hypervisor, which acts as a platform for running multiple VMs. 

Each VM runs its own operating system, completely isolated from the host and other VMs, thereby ensuring thorough compartmentalization of applications.

There are two types of hypervisors:
1. **Type 1 (Bare Metal):** This hypervisor runs directly on the hardware of the host machine, offering high performance and efficiency because it interacts directly with the physical hardware.
2. **Type 2 (Hosted):** This version runs as an application within the host operating system, relying on the host OS to manage hardware resources. 

I have a Proxmox server, running for 6+ years, that is a type-2 hypervisor and it works beautifully. It is a lot of fun and cannot recommend enough. You will learn so much and go through many rabbit holes :) .

## What is Docker Containers

Docker containers, on the other hand, offer a more lightweight approach to application deployment. Unlike VMs that virtualize the entire operating system, containers virtualize at the application level, sharing the host system's kernel but running isolated processes. 

This architecture allows Docker containers to be extremely efficient and fast, making them ideal for environments where rapid scaling is required.

## Key Differences Between VMs and Docker Containers

1. **Isolation:** VMs provide complete OS isolation, which enhances security and allows different operating systems to coexist on the same physical machine. Containers, while isolated, share the host's kernel, making them less isolated compared to VMs but more resource-efficient.

2. **Performance:** VMs involve additional overhead due to the need to emulate a full hardware environment and an entire OS for each instance. Containers, being lighter, start faster and use fewer resources, as they only need to run the application and its dependencies.

3. **Resource Management:** VMs require a predefined allocation of resources, which can lead to underutilization or resource scarcity if not managed properly. Containers are more flexible, allowing for dynamic resource allocation that maximizes efficiency.

4. **Portability:** Containers excel in portability since they encapsulate the application configuration and dependencies in a single package, making it easy to move across different environments that support Docker.

5. **Use Cases:** VMs are better suited for applications requiring full isolation and security, complete control over the operating environment, or where hardware emulation is necessary. Containers are ideal for microservices architecture, application scaling, and rapid development cycles where quick deployment and efficient resource use are priorities.

## Practical Applications and Considerations

In practice, the choice between a Docker container and a VM depends on the specific needs of the application and the environment. 

For instance, if you need a fully isolated environment for secure data processing, a VM might be the appropriate choice. 

However, if you are developing microservices that need to scale dynamically, Docker containers can provide the necessary speed and flexibility.

## Conclusion

Both Docker containers and virtual machines offer significant advantages but serve different purposes based on application requirements and environmental context.

I recommend that you start playing with docker because it really is a great tool to use and doesn't consume that many resources.

## Connect with me

If you like this article be sure to leave a comment. That will make my day!

If you want to read other stuff by me you can check out my [personal blog](www.goncaloalves.com). 

If you want to connect with me you can send me a message on [Twitter/X](twitter.com/iamgoncaloalves). 

You can also check other stuff that I have going on [here](bio.link/goncaloalves)