DevOps teams are under increasing pressure to deliver greater functionality to customers quickly. Cloud providers offer a solution through scalable platforms with excellent CLI and API integrations. Unfortunately, the interfaces exposed by cloud providers can be incompatible. But some cloud-native tools help DevOps teams build custom solutions for any cloud provider.

Two such tools are Vagrant and Docker. Understanding what they do and how they work together is key to getting the most out of your cloud environment.

Vagrant is a tool for building and managing virtual machine (VM) environments in a single workflow. Whether you’re building local VMs to run with hypervisors or just cloud VMs, Vagrant provides a consistent configuration format, single CLI, and shared provisioners to install software and alter configurations.

Docker provides the ability to package software and supporting configuration into images that are run consistently across multiple platforms. Docker allows DevOps teams to have confidence that software will run on a local workstation in much the same way it runs on a managed cloud platform.

Although Vagrant and Docker share the goal of creating repeatable environments, they do so in different yet complementary ways. This article explores how Vagrant and Docker achieve their goals.

Check Out Our Video Guide on Vagrant vs Docker:

What Is Docker?

Docker is an open platform for developing, shipping, and running applications. It allows DevOps teams to package custom software, supporting applications, and libraries, as well as instructions for configuration, networking, file mounts, health checks, and launch scripts into a self-contained artifact called an image.

Images are then executed in a lightweight, isolated environment called a container. Unlike VMs, which typically reserve fixed amounts of memory and strictly limit CPU usage, containers are much more elastic and consume only the memory and CPU they need.

This allows many containers to run side by side efficiently, reducing costs and increasing scale. However, containers are not considered as secure as VMs, meaning containers aren’t an ideal solution for running untrusted code.

Screenshot: Docker website — Vagrant vs Docker.
Docker

Why Use Docker (vs Vagrant)?

DevOps teams typically use Docker to deliver and run web applications, including cloud-native applications that either integrate with message queues or are hosted on function-as-a-service (FaaS) platforms responding to cloud-based triggers and events.

Platforms like Kubernetes build on Docker, allowing containers to be orchestrated at scale. In addition, every cloud provider supports the ability to run Docker containers in their platform as a service (PaaS) offerings. It’s now rare to find a PaaS that doesn’t support Docker.

Docker can also distribute and execute CLI-based tooling, with all popular cloud-native tools offering Docker images embedding their CLI.

What Are the Benefits of Docker?

Docker solves a common issue faced by traditional software deployment processes, where custom applications could exhibit different results when run on a developer’s local workstation and in a production environment. These differences are often the result of developers running different versions of programming languages, having different configurations for supporting applications like web servers, or running completely different operating systems than the production environment. These differences lead to support issues bouncing back and forth between teams with comments like, “It works on my machine.”

As Docker images include the code for custom applications, all supporting applications, and libraries, the images execute in containers that provide more consistent results regardless of where they’re run.

Docker images are easy to distribute using registries like Docker Hub, allowing DevOps teams to share images amongst themselves quickly and deploy the images using the various hosting platforms. For example, you’ll find Docker behind Kinsta’s DevKinsta WordPress development suite.

Where more complex application stacks are required, like deploying a database alongside a backend application, or a collection of related microservices, Docker Compose provides the ability to create and link multiple Docker containers with a single command. All containers, associated settings, and networking configurations are defined in a single YAML file. Docker Compose then reads this file to create and manage multiple containers as a single unit.

Docker also enjoys near-universal support across the major cloud providers, meaning DevOps teams are spoiled for choice when executing their Docker images in the cloud.

What Is Vagrant?

For decades, VMs have proven a reliable and secure solution for partitioning and provisioning computing resources. VMs remain the most popular services exposed by cloud providers and many commercial and open-source solutions for teams are looking to run VMs on-premises.

However, this variety poses a challenge for DevOps teams. Each solution exposes a different CLI and API to create and manage VMs, making it difficult to maintain consistency between local development environments and cloud-based platforms.

Vagrant offers a solution by abstracting the underlying differences between VM platforms, allowing DevOps teams to provision new VMs with a single CLI and consistent configuration syntax. Teams that adopt Vagrant can create consistent VMs regardless of whether they’ll run on-premises or in a cloud environment and can easily switch between environments. Vagrant has a multitude of different uses, including as a MAMP replacement (macOS, Apache, MySQL/MariaDB, and PHP, Perl, or Python).

Screenshot: Vagrant website — Vagrant vs Docker.
Vagrant

Why Use Vagrant (vs Docker)?

It’s possible to boot a VM and manually configure the operating system by manually installing software and editing configuration files. This isn’t ideal, as the process isn’t repeatable, meaning VMs must be manually recreated for different operating systems and providers. It also makes it nearly impossible to understand how a VM was configured at a later date.

A better practice is to automate the process required to configure a virtual machine. Vagrant provides a single tool for automating the process of constructing VMs for many providers. Vagrant also provides a large selection of high-quality VMs on Vagrant Cloud that DevOps teams can use to begin their own VMs.

What Are the Benefits of Vagrant?

DevOps teams utilizing Vagrant have a single tool to learn regardless of whether they’re building VMs for on-premises platforms, cloud platforms, or both. You can easily tweak Vagrant configuration files and rerun or review them at a later date to understand how a VM was constructed.

Vagrant also allows DevOps teams to migrate to the cloud or between providers without redesigning the process they use to build VMs.

The VMs created by Vagrant provide a high level of isolation, often taking advantage of specialized virtualization support built into modern CPUs. This makes VMs, and tools that create them, like Vagrant, the best choice where security and isolation are a high priority.

Many Linux distributions, such as Ubuntu and Fedora, also provide official Vagrant boxes on which DevOps teams can build. This reduces the time it takes to build custom VMs.

Vagrant vs Docker: A Closer Look

Both Vagrant and Docker make DevOps teams more efficient by automating the building, distributing, and running of software applications.

Docker achieves this goal with a custom packaging format in the form of images and a lightweight execution environment via containers. Containers execute consistently between platforms, giving DevOps teams greater confidence that their software works as expected. Docker is well supported by PaaS and FaaS cloud platforms, allowing DevOps teams to choose the best platform for their needs.

Many containers can co-exist on a single host, primarily — but not completely — isolated from one another while sharing the same pool of resources. This allows containers to scale efficiently.

Vagrant achieves this goal by providing a consistent method for constructing VMs with existing providers. It’s an ideal choice for teams that have already invested in VMs, relying on their high level of isolation, security, control, and customization.

Docker also provides the best solution when running trusted code while minimizing computing costs. Containers are reasonably isolated from one another but incur very little overhead. That means many containers can run simultaneously on a shared OS. VMs provide the best solution when teams need high levels of isolation and security or require the ability to run many different operating systems side by side, with Vagrant providing a convenient solution for building VMs in an automated and repeatable manner.

Vagrant and Docker are not mutually exclusive technologies, and both tools can be used side by side. For example, DevOps teams may use Docker to develop and run applications while using Vagrant to recreate specialized environments to reproduce issues. Docker may even be run inside a VM created by Vagrant, perhaps to test new versions of Docker or to test tooling distributed as Docker images in an isolated environment.

Summary

DevOps teams have many choices when building, distributing, and running applications.

Docker provides a custom image format and container execution environment that allows large-scale and efficient resource usage and is supported across PaaS and FaaS platforms.

VMs offer secure and isolated execution environments from many cloud and on-premises VM providers, with Vagrant abstracting away many differences to provide a single CLI and consistent configuration syntax to instantiate VMs across providers.

Before you start developing your next app, take a look at how Kinsta’s Application Hosting service can take advantage of Dockerfiles to manage the deployment of your code.

Salman Ravoof

Salman Ravoof is a self-taught web developer, writer, creator, and a huge admirer of Free and Open Source Software (FOSS). Besides tech, he's excited by science, philosophy, photography, arts, cats, and food. Learn more about him on his website, and connect with Salman on Twitter.