Vagrant vs Docker: what is better and when?

Setting up a separate server for a single task is wasteful. Agile software development methodology dictates the need to be able to build and adjust virtual servers quickly, instead of working with rigid configurations of manually-configured hardware and software. Vagrant was the first answer to this need, Docker became the best. We compare Vagrant vs Docker and describe the best uses for each of these tools.

Vagrant and Docker: similarities and differences

First of all, let’s make it clear: Vagrant and Docker serve the same purpose but operate using quite different methods.

Vagrant is a free open-source software from HashiCorp written in Ruby. It is a framework for creating and controlling virtual machines (VMs), used as an envelope for VirtualBox, Salt, Puppet, Chef, Ansible, etc. Using Vagrant ensures the developers can configure exactly the same environment they need to replicate the conditions of the production environment.

Docker is a free open-source software from Docker, Inc. written in Go. It’s a tool for creating and managing containers — the code envelopes with all the needed runtime, so that a containerized app can run on any underlying infrastructure with Docker installed.

There are many reasons to choose between Vagrant or Docker, and these depend largely on how you plan to use their strong and weak sides. For example, when comparing Docker vs Vagrant, one must consider the following differences:

  • different scopes of app isolation
  • different levels of built-in security
  • huge differences in speed of launch
  • levels of resource consumption
  • limits of operational capacities

Below we will take a closer look at these points.

Scopes of app isolation

Vagrant operates VMs that run atop the hypervisor layer. They share the physical resources of the server. Every VM has to have an OS and all libraries and software needed to run the app. All of these depend on the hypervisor layer, and if the physical server resources are exhausted — the system begins to slow down, lag and freeze.

With Docker — the containers are run atop Docker, omitting the need of an OS to sustain them. This frees up so much resources, that multiple containers can actually run atop a single VM. In terms of app isolation — all the containers are isolated from each other and use little resources, so several containers can run atop a single VM.

Levels of built-in security

Virtual Machines run atop the OS and hypervisor and are managed from there. Thus said, compromising access to the master OS can allow to hamper the work of all guest OS’s on all virtual machines.

Quite the contrary — containers are isolated from underlying infrastructure by their architecture. Even if a single container is compromised (which is not easily done) — the rest will not suffer from it.

Speed of launch

When we compare Vagrant vs Docker in terms of speed of launch, Docker containers win by a huge margin. VM needs time to launch, setup the guest OS, install and configure all the needed software. Depending on the system configuration required, the process can take anywhere from 6 minutes to half an hour.

Containers, on the other hand, are composed from ready images with a single dockercompose command. Once the image is built, any number of containers can be launched from it within seconds. Kubernetes and Docker Swarm allow spinning up clusters of thousands of containers within seconds.

In addition, if a container fails it can be rebooted in seconds. A VM must be stopped and fully rebooted, which can take several minutes.

Resource consumption levels

As we mentioned before, a VM needs a certain amount of resources to function on its own, and some must be devoted to the hypervisor. Depending on the type of virtualization, this can amount from 5% to 15% of overall server resources.

Containers, on the other hand, don’t require any resources to run. Well, they do, but it amounts to 1%-1,5% of server capacities at best and is by far lower than the resource consumption of virtual machines. Actually, a host running Docker containers can provide 300% increased efficiency, as compared to running VMs.

Limits of operational capabilities

Docker containers run atop any infrastructure if Docker is installed. VMs can be launched atop any OS. As this is one of their main operational requirements, both tools deal with it well. However, one can run Vagrant using Docker compose, as there are lots of Docker images with various Vagrant environments readily available in the registry. Launching a Vagrant VM with Docker will require a full-scale configuration, however.

Conclusions on comparing Vagrant vs Docker

To wrap it up, Vagrant is a great tool for software development. It allows creating exact copies of the production environments for the purposes of bug fixing. However, running thousands of VMs in production is not resource efficient, nor is it advisable.

Docker, on the other hand, is meant to support heavy workloads in production. It is centered at launching and managing thousands of containers quickly, transparently and reliably.

Therefore, Vagrant can be a decent choice for software development, while Docker is definitely the way to go for production.