Docker vs Virtual Machine (VM) - Key Differences explained
When I started learning Docker, one of my first questions was: "What's the difference between Docker and Oracle VirtualBox?" 🤔
So in this blog, I'll explain the difference by showing you how Docker works on an operating system level and comparing it to how a virtual machine works.
To understand how Docker works, let's first look at how an operating system is made up. An operating system has two layers: the operating system kernel and the applications layer:
The kernel communicates with hardware components like the CPU and memory. However, the applications run on top of the kernel.
For example, Linux OS has many distributions, like Ubuntu, Devian, Mint etc. and each has different graphical user interfaces, file systems, and applications. So even though they use the same Linux kernel, they implement different applications on top of that kernel.
Both Docker and virtual machines (VM) are virtualization tools. So the question is...
What Parts of the Operating System does Docker and VM Virtualize?
Docker virtualizes the applications layer of the operating system:
When you download a Docker image, it contains the applications layer of the OS and other applications installed on top of it. Accordingly, Docker uses the host's kernel, because it doesn't have its own.
VirtualBox or a Virtual Machine
VirtualBox, or a virtual machine, on the other hand, has its own kernel and application layer. So it virtualizes the entire operating system. So it boots up its own kernel when you download a virtual machine image on your host.
What Does This Difference Between Docker and Virtual Machine Mean?
Let's explore some of the significant differences between Docker and virtual machines.
1. Image Size
The difference between Docker and virtual machines means that Docker images are much smaller and faster than virtual machine images. The reason is that the Docker images have to implement just one layer.
Docker images are usually a couple of megabytes, while virtual machine images can be a couple of gigabytes.
Docker containers also start up much faster than virtual machines, because they only need to start the applications layer. In contrast, virtual machines need to start the entire operating system.
However, there is a compatibility issue with Docker. You can run a virtual machine image of any operating system on any other operating system host, but you can't do that with Docker, at least not directly.
So what is the problem here?
Let's say you have a Windows operating system with Windows kernel and its application layer and you want to run a Linux based Docker image directly on that Windows host.
The problem here is that Linux based Docker image cannot use the windows kernel. It would need a Linux kernel to run, because you can't run a Linux application layer on a Windows kernel, so that's kind of an issue with Docker.
However, when you're developing on Windows or Mac OS, you want to run various services, because most containers for the popular services are actually Linux based. Also interesting to note that Docker was originally written and built for Linux, but later Docker actually made an update and developed what's called "Docker Desktop for Windows" and Mac, which made it possible to run Linux based containers on Windows and Mac computers as well.
So the way it works is that Docker Desktop uses a hypervisor layer with a lightweight Linux distribution on top of it to provide the needed Linux kernel and this way make running Linux based containers possible on Windows and Mac operating systems
And by the way if you want to understand more about virtualization and how virtual machines work and what a hypervisor for example is, you can watch my video where I explain all of that in detail: Virtual Machines explained in 15 Mins
So this means for local development as an engineer, you would install Docker Desktop on your Windows or Mac OS computer to run Linux based images, which as I mentioned most of the popular services, databases etc are mostly Linux based, so you would need that. 💡
Watch this Docker Crash Course to learn everything about Docker to get started and use it in practice 🚀
You can learn more about Docker and other DevOps technologies on my Youtube channel 👏
Like, share and follow me 😍 for more content: