Getty Images/iStockphoto

Evaluate Weigh the pros and cons of technologies, products and projects you are considering.

Vagrant vs. Docker: How these cloud-native tools differ

Both Docker and Vagrant are important parts of a cloud-native stack, but they have very different roles when it comes to supporting containers and VMs within the CI/CD process.

Vagrant and Docker both provision resources needed to support cloud-native deployments, though they focus on different parts of the deployment stack. Developers use Vagrant to provision VMs upon which a Linux container can run. Docker is itself one of the containers that can be installed on one of these VMs created by Vagrant.

Essentially, Docker is a technology for creating and running Linux containers, and Vagrant is a machine provisioning tool used to create VMs and then populate them with applications. In other words, you use Vagrant to create a VM and install Docker. Then, once Docker is installed, that VM can run containers.

Understanding Docker

Docker is a technology that is used to create and run containers. A container is a collection of one or more processes, organized under a single name and identifier that are isolated from the other processes running within a computing environment. That computing environment can be a physical computer or a VM.

Docker technology has two main components: the client command-line interface (CLI) tool and the container runtime. The CLI tool is used to execute instructions to the Docker runtime at the command line. The job of the Docker runtime is to create containers and run them on the operating system.

docker container images
A container image is a template upon which an actual container is realized at runtime.

Docker uses two main artifacts that are essential to container technology. One is the container image. The other is an actual container. A container image is a template upon which a container is realized at runtime, as shown in Figure 1 to the left.

A container has no life of its own outside of the operating system. Thus, in terms of an automated CI/CD process and in order for Docker to work, a real or virtual machine with an OS must exist. Also, that machine must have the Docker runtime and daemon installed. This is where Vagrant comes into play.

Understanding Vagrant

In order to support CI/CD in an automated deployment environment, you need a way to run scripts that create VMs, and then populate them with an operating system and other applications that are required to fulfill the VMs' purpose. The way you do this is by using a provisioning tool. Vagrant is one such tool; however, there are others that perform similar tasks, such as Ansible and Terraform.

Under Vagrant, a provisioning script is named, Vagrantfile. The following shows a sample Vagrantfile:

# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure(2) do |config| = "generic/ubuntu1904"

    # Install Docker
   config.vm.provision :docker

You execute a Vagrantfile script using the command:

vagrant up

When you run this command, by default, Vagrant looks in the current directory for the Vagrantfile and executes the instructions in the file. Typically, the instructions include creating one or many VMs, installing an operating system on the machines, and then adding the other required applications, such as the Docker components.

Figure 2 below is an illustration that conceptually describes a Vagrant workflow process.

Vagrant Docker workflow
This shows a workflow that uses Vagrant to create a virtual machine and then provisions it with an operating system, Docker and the Kubernetes container orchestration framework.

The important difference between Vagrant vs. Docker is that Docker is used to create and run Linux containers, while Vagrant does the work to provision a machine with an operating system, a Docker installation and any other application that needs to run on the OS.

Dig Deeper on Java DevOps