Comparing Docker Containers Vs Virtual Machines

Comparing Docker Containers Vs Virtual Machines

If you’re a programmer or techie, chances are you’ve at least heard of Docker: a helpful tool for packing, shipping, and running applications within “containers.” It’d be hard not to, with all the attention it’s getting these days — from developers and system admins alike.

What are containers and why do you need them?

Containers are a solution to the problem of how to get software to run reliably when moved from one computing environment to another. This could be from a developer's laptop to a test environment, from a staging environment into production, and perhaps from a physical machine in a data center to a virtual machine in a private or public cloud.

Let’s understand it with an example:

A company needs to develop a Java Application. In order to do so the developer will setup an environment with tomcat server installed in it. Once the application is developed, it needs to be tested by the tester. Now the tester will again set up tomcat environment from the scratch to test the application. Once the application testing is done, it will be deployed on the production server. Again the production needs an environment with tomcat installed on it, so that it can host the Java application. If you see the same tomcat environment setup is done thrice. There are some issues that I have listed below with this approach:

  • There is a loss of time and effort.
  • There could be a version mismatch in different setups i.e. the developer & tester may have installed tomcat 7, however the system admin installed tomcat 9 on the staging server.


Now, I will show you how Docker container can be used to prevent this loss.

In this case, the developer will create a tomcat docker image ( A Docker Image is nothing but a blueprint to deploy multiple containers of the same configurations ) using a base image like Ubuntu, which is already existing in Docker Hub (Docker Hub has some base docker images available for free) . Now this image can be used by the developer, the tester and the system admin to deploy the tomcat environment. This is how docker container solves the problem.

However, now you would think that this can be done using Virtual Machines as well. However, there is catch if you choose to use virtual machine. Let’s see a comparison between a Virtual machine and Docker Container to understand this better on 3 main parameters - Size , Boot Time and Integration with other systems.

Size

Consider a situation - I have a host system with 16 Gigabytes of RAM and I have to run 3 Virtual Machines on it. To run the Virtual Machines in parallel, I need to divide my RAM among the Virtual Machines. Suppose I allocate it in the following way:

  • 6 GB of RAM to my first VM,
  • 4 GB of RAM to my second VM, and
  • 6 GB to my third VM.

In this case, I will not be left with anymore RAM even though the usage is:

  • My first VM uses only 4 GB of RAM – Allotted 6 GB – 2 GB Unused & Blocked
  • My second VM uses only 3 GB of RAM – Allotted 4 GB – 1 GB Unused & Blocked
  • My third VM uses only 2 GB of RAM – Allotted 6 GB – 4 GB Unused & Blocked

This is because once a chunk of memory is allocated to a Virtual Machine, then that memory is blocked and cannot be re-allocated. I will be wasting 7 GB (2 GB + 1 GB + 4 GB) of RAM in total and thus cannot setup a new Virtual Machine. This is a major issue because RAM is a costly hardware.

So, how can I avoid this problem?

If I use Docker, my CPU will allocates exactly the amount of memory that is required by the Docker Container.

  • My first container will use only 4 GB of RAM – Allotted 4 GB – 0 GBUnused & Blocked
  • My second container will use only 3 GB of of RAM – Allotted 3 GB – 0 GB Unused & Blocked
  • My third container will use only 2 GB of RAM – Allotted 2 GB – 0 GBUnused & Blocked

Since there is no allocated memory (RAM) which is unused, I save 7 GB (16 – 4 – 3 – 2) of RAM by using Docker Container. I can even create additional containers from the leftover RAM and increase my productivity.

So here Docker Container clearly wins over Virtual machine as I can efficiently use my resources as per my need.

Start-Up

When it comes to start-up, Virtual Machine takes a lot of time to boot up because the guest operating system needs to start from scratch, which will then load all the binaries and libraries. This is time consuming and will prove very costly at times when quick startup of applications is needed. In case of Docker Container, since the container runs on your host OS, you can save precious boot-up time. This is a clear advantage over Virtual Machine.

Consider a situation where I want to install two different versions of Ruby on my system. If I use Virtual Machine, I will need to set up 2 different Virtual Machines to run the different versions. Each of these will have its own set of binaries and libraries while running on different guest operating systems. Whereas if I use Docker Container, even though I will be creating 2 different containers where each container will have its own set of binaries and libraries, I will be running them on my host operating system. Running them straight on my Host operating system makes my Docker Containers lightweight and faster.

So in short life is easier with a local docker repository and we can pull things we know we need (nginx,redis, elk, kafka, flink etc.) and still develop on a plane with little to no internet without a problem and not have machine grind to a halt like when we run a heavier set of VMs that suck battery dry and make the whole machine less responsive.



To view or add a comment, sign in

More articles by Maninder Narang

Others also viewed

Explore content categories