Learning Library

← Back to Library

Docker vs Virtual Machines: Key Differences

Key Points

  • Docker and virtual machines both enable virtualization, but VMs emulate entire physical hardware via a hypervisor while Docker containers share the host OS and virtualize only the operating system layer.
  • A hypervisor sits on physical hardware and allocates resources to multiple VMs, each running its own full guest OS and virtual hardware such as CPU and storage.
  • Docker uses the Docker Engine to manage containers, leveraging Linux kernel features like cgroups for resource limits and namespaces for isolation, so containers contain just the application and its dependencies.
  • Docker images—built from Dockerfiles—are lightweight, portable packages that instantiate as isolated containers, making them ideal for microservices, rapid development, and scaling, whereas VMs are better suited for workloads requiring full OS isolation or legacy applications.

Full Transcript

# Docker vs Virtual Machines: Key Differences **Source:** [https://www.youtube.com/watch?v=a1M_thDTqmU](https://www.youtube.com/watch?v=a1M_thDTqmU) **Duration:** 00:08:40 ## Summary - Docker and virtual machines both enable virtualization, but VMs emulate entire physical hardware via a hypervisor while Docker containers share the host OS and virtualize only the operating system layer. - A hypervisor sits on physical hardware and allocates resources to multiple VMs, each running its own full guest OS and virtual hardware such as CPU and storage. - Docker uses the Docker Engine to manage containers, leveraging Linux kernel features like cgroups for resource limits and namespaces for isolation, so containers contain just the application and its dependencies. - Docker images—built from Dockerfiles—are lightweight, portable packages that instantiate as isolated containers, making them ideal for microservices, rapid development, and scaling, whereas VMs are better suited for workloads requiring full OS isolation or legacy applications. ## Sections - [00:00:00](https://www.youtube.com/watch?v=a1M_thDTqmU&t=0s) **Docker vs Virtual Machines Explained** - The speaker defines virtual machines and Docker containers, explains their underlying virtualization mechanisms (hypervisor vs OS‑level), and offers guidance on which workloads are best suited for each technology. - [00:03:07](https://www.youtube.com/watch?v=a1M_thDTqmU&t=187s) **Docker vs. VM Fundamentals** - The passage explains Docker images and containers as lightweight, self‑contained packages and contrasts them with virtual machines, describing hypervisor types and virtual hardware components. - [00:06:15](https://www.youtube.com/watch?v=a1M_thDTqmU&t=375s) **VM vs Docker Use Cases** - The speaker contrasts VM isolation and legacy support with Docker’s strengths in microservices, rapid development, and resource efficiency. ## Full Transcript
0:00What's the difference between Docker and virtual machines, or VMs? 0:04Well, we're going to 1) define these two technologies then 2) describe how they work 3) give a bit of guidance on which workloads fit best for each technology. 0:18Now, both of these technologies address something in common and that thing in common is called virtualization. 0:28And what I mean by virtualization is a process where software is used to create an abstraction layer. 0:36So for VMs, that abstraction layer, or that abstraction software, is called a hypervisor. 0:47And a hypervisor is, simply put, something that helps a virtual machine emulate a physical computer. 0:55So underneath the hypervisor here we have some hardware. 1:02And the hypervisor manages the allocation of resources between different virtual machines on that single physical host. 1:10So up here we have a number of VMs. 1:16Now each VM runs its own operating system and it has its own virtual hardware. 1:22So like virtual CPU, virtual storage, that sort of thing. 1:26Now, what about Docker? 1:28Well, Docker is an open source platform that uses containerization technology. 1:33It allows developers to package applications and their dependencies into lightweight, portable containers. 1:45Instead of virtualizing the underlying hardware like this hypervisor is doing for the VMs, Docker containers virtualize the operating system. 1:58So each individual container contains only the application and its libraries and dependencies. 2:05So let's break down the main components of both solutions, and we'll start with Docker. 2:11So the first component I want to tell you about is called the Docker engine. 2:18Now, this is the core software that's responsible for managing the lifecycle of Docker containers. 2:23So we're talking about things like providing the infrastructure for creating, running and orchestrating the containers. 2:30And the Docker engine interacts with the host kernel to allocate resources and enforce isolation between containers. 2:37And that's done through two things primarily; there's something called cgroups or control groups, and they allocate resources among the processes. 2:47And then there's something else called namespaces, and namespaces restrict a container's access and visibility to other resources on the system. 2:57And that ensures that each container has its own isolated environment. 3:02Now there's also Docker images. 3:09And Docker images are lightweight, standalone and executable packages that include everything you need to run a piece of software. 3:17So we're talking about the code for the software, the runtime, the system tools, the libraries and any settings that we need as well. 3:24And Docker images are built using Docker files, which are very simple documents which provide instructions for creating the image. 3:38And then there's also the Docker containers themselves. 3:46And these are the instances of the images that run in the Docker engine. 3:54Each container is an isolated and self-sufficient environment that includes only the necessary components 4:00for running a specific application and can be quickly started, stopped and restarted. 4:06Now, for VMs, we already know the hypervisor is the software responsible for creating, managing and running these virtual machines. 4:14And hypervisors come in two types: so we have a Type 1 hypervisor, that's also known as a bare metal hypervisor. 4:25And that runs directly on the host hardware, so right on that computer there. 4:29There is also a Type 2, and Type 2 is considered a hosted hypervisor, and that's where the hypervisor runs on top of an existing operating system. 4:41Now, the other components we should be concerned about, well, there's the virtual hardware. 4:47And this refers to the emulated components of a virtual machine. 4:51So I'm talking about things like a virtual CPU, virtual memory, virtual storage, virtual network interfaces. 4:58And these components are presented to the guest operating system as if they were real hardware. 5:04The hypervisor is responsible for managing and allocating these virtual resources to each VM. 5:10And yeah, speaking of guest OS, that's another core part of all of this, the guest operating system. 5:17Those are the individual operating systems that run inside each virtual machine. 5:20And each VM can have its own guest OS, which may differ from the host OS and other VMs on the same host. 5:27And that allows users to run multiple operating systems and applications on the same physical machine. 5:35Now, when to pick one over the other? 5:38While the choice isn't always black and white, let's start with some common use cases for VMs. 5:47When would you use a virtual machine? 5:48And I can think of three right away. 5:51And number 1, really, the obvious one, is the diverse operating systems that I just mentioned. 5:58VMs let you run different operating systems, so we could have a VM running Windows, another VM running Linux, all on this same piece of hardware. 6:05And that's handy when you're testing applications across multiple platforms, for example. 6:10A second big advantage is isolation. 6:16Now both solutions offer levels of isolation, but with the VMs, 6:19isolation is it's pretty much built in since each VM runs its own separate kernel and operating system. 6:27And then number 3 is legacy, and what I mean by that, is that VMs are well-suited for running legacy applications 6:36that rely on specific operating system versions or configurations that might not be compatible with the host OS or the other containers. 6:44So you can set up an environment that is perfect for that legacy application and not have to change it. 6:50Now, what about Docker containers? 6:52Let's think about some use cases for these. 6:56And first and foremost, top of the list, is microservices. 7:02This is probably the most common use for containers-- that Docker containers lightweight nature, 7:08its fast start up times and it's the ability to package and distribute dependencies, make it ideal for managing microservices-based applications. 7:17Number 2, it's just the speed of things. 7:20So the rapid development and deployment in the ability to use the Docker containers because we can quickly build, deploy and scale up these containers 7:32and that makes them well-suited for agile development practices and continuous integration/continuous deployment, or CI/CD pipelines. 7:41And then 3, on a similar theme, is resource efficiency. 7:48Which is just to say that containers share the same host kernel and have a much smaller footprint than VMs, 7:54and that allows more containers to run on the same piece of hardware with less overhead. 8:00So, look, in selecting between Docker and VMs, essentially you'll need to consider the specific needs of your applications and infrastructure. 8:09And it's common to see both technologies used in hybrid environments where legacy applications might run on VMs 8:18and then modern microservices-based applications, they'll probably use Docker containers. 8:25It's really not an either or choice for organizations. 8:30Commonly, you'll see both. 8:31And either way, these virtualization technologies have transformed the way we deploy and manage applications.