Before we dive in deep in Docker, let's look into the difference between Virtual Machines & Docker containers and try to understand more about docker.
In this article, we’ll look into the key concepts of what the Virtual machine is & what makes Docker containers different & We’ll go through on how docker works under the hood & look on the key concepts of docker.
Docker Containers are not VMs
Every developer at some stage of their development career have mixed docker containers with Virtual machines & related to them and thought they are pretty much the same, but the truth is they
You can think of
Docker containers as isolated processes
Let’s analyze both VMs and Docker containers one by one & see the difference ourselves while comparing both side by side.
We’ll assume of running multiple applications on a server & how they operate on the VM and Docker container.
Running a VM on a server is explained in the following diagram. We’ll go through it step by step.
It all begins with some sort of
infrastructure, it could be your laptop, a dedicated server running in a data center or a virtual private server that you’re using on cloud, such as a digital ocean droplet or an Amazon ec2 instance.
On top of that, we’ve an
operating system, for example MACOSX, Linux or windows etc.
Then, we’ve a thing called the
Hypervisor. You can think of virtual machines as self-contained computers packed into a single file but something needs to be enabled to run this file & that’s where hypervisor comes into play. There are basically two types of Hypervisors.
- Type 1 Hypervisor – which interface directly to the infrastructure. Few examples include: HyperKit(OSX) / Hyper-V (windows)
- Type 2 Hypervisor – which works as an application on top of the host operating system. Few examples include: VirtualBox / VMWare
Usually Type 1 hypervisors are more efficient as they can bypass the operating system & interact directly with the hardware of the server.
On top of it, we’ve several Guest operating systems which are controlled by the hypervisor. They could be the same OS or could be different depending on the requirements. But the problem here is if each guest OS is of 700MB. It makes 700MB x 3 = 2.1GB disk space in addition to CPU & Memory resources. It gets worse upon more operating systems adding to the server & there’s a lot waste happening here.
Each operating system have their own set of binaries and libraries which are required to run your applications. For example: we need a PostgreSQL library to run on one guest OS and MongoDB. One OS requires python, other requires nodejs and so on.
Finally we’ve our applications, where the source code resides & we’ve to run each of our apps inside of separate guest operating systems.
Now let’s compare that to a Docker container & see the difference.
Consider docker containers as magic bullets. We’ll explain each step in the following diagram of a docker container.
Docker containers also have some kind of a infrastructure like VMs such as your laptop or some virtual server in the cloud.
On top of it, we’ve our host operating system. This could be any OS you want which is capable of running docker. Almost all distributions of Linux are supported & MacOS and windows too.
On top of it, its the Docker Daemon, which replaces the hypervisor. It’s the service that runs in the background on your host operating system & manages everything to run required to run a docker container.
Next is our binaries and libraries just as in VMs. But instead of running inside of the guest OS, they are special packages packed inside of docker images & Docker daemon runs these images.
And the last piece of the puzzle is our applications on the top, where each one utilizes their own set of docker images and will be managed by the Docker daemon. Typically each application & their binaries and libraries dependencies packed into the same docker image, hence each application is isolated.
It’s also noticeable that docker containers boot up in milliseconds whereas VMs takes minutes to boot up. We can also save ton of disc space and resources while using docker containers as compare to VMs which waste more resources and adding up a number of guest operating systems in the server.
When should we use Docker containers or Virtual Machine?
You can use virtual machines when you either own a some physical servers that’d be in your best interest or when you need to test entire systems in specific environment.
If you want to isolate a single process than docker is a great fit. Please note that docker is not only for web applications. You can run almost any process inside of docker. For instance if you’re on macOS or windows and want to use any tool from linux, with the aid of docker, you can get your hands on it while using your current OS. Great isn’t it?
Are VM and Docker compatible?
VM and Docker are pretty much compatible with each other. Let’s see the diagram below where we require to test out some firewall configuration settings in ubuntu linux while on a MacOSX or windows machine.
We can build our applications once and can deploy it anywhere from amazon ec2 to digital ocean droplet to azure to google cloud or any other major cloud provider. With docker, you have tremendous amount of freedom and flexibility. you can pick the infrastructure that suits your application requirements.