An introduction to Vagrant

Bowen Zhao
3 min readNov 11, 2019

--

What is vagrant?

According to official instruction: Vagrant is a tool for building and managing virtual machine environments. We can consider it as a too that enable use to get access to the virtual machine/aws ec2/ docker by command line.

The structure of virtual machine

Suppose right now, we want to develop several project, each of them works on different environment: MacOS, Windows, Linux, or even AWS. We have to install different types of VM software, and each time switch to one environment. But with the help of Vagrant, we can get access to all of them by just opening 4 terminal.

Vagrant is composed of three part: Basic image + configurations + command line interface(ssh)

Install:

Vagrant uses Virtualbox to manage the virtual dependencies.

$ brew cask install virtualbox

THEN install Vagrant.

$ brew cask install vagrant

Add Box: Box can be considered as the guest operation system, by adding box, we create a basic virtual environment. The box can be reused to set up more virtual machines.

# add a box
vagrant box add ubuntu/trusty64
# check existing box
vagrant box list
# update a box
vagrant box update --box box_name
# remove a box
vagrant box remove box_name

more box can be found from https://app.vagrantup.com/boxes/search

Configurate Virtual Machine: In order to use the box, we need to add more information to a Vagrantfile.

The basic syntax:

Vagrant.configure(“2”) do |operation|end

"2" represents the version of the configuration object.

  1. configure the vm

Here is an example of how to configure vm:

Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/trusty64" # base image of box
config.vm.box_version = "1.1.0". # box version
config.vm.box_url = "https://...." # the source url of box

2. install software through provisioning:

# Configure through shell
Vagrant.configure("2") do |config|
config.vm.provision "shell" do |s|
# config.vm.provision provision_name, type: “shell” do |s|
s.variable = value
end
end
# Configure docker
Vagrant.confgure("2") do |config|
config.vm.provision "docker" do |docker|
docker.build_image "/vagrant/app" # build image from folder
docker.pull_iamges "ubuntu" # build image pulling from hub
docker.run container_name
end
end

Configure Networking:

  1. by configuring forwarded ports, you can post your host machine’s data to a port on gust machine.
  2. by configuring private network, you can access your guest machine that is not accessible to public
Vagrant.configure("2") do |config|
config.vm.network "forwarded_port", guest:5000, host:5001
config.vm.network "private_port", ip:0.0.0.1

Configure Synced folder: Synced a folder on the host machine to the guest machine.

Vagrant.configure("2") do |config|
config.vm.synced_folder "/app" "/vagrant" # host to guest folder
end

Configure SSH: configure how vagrant can access to your vm using ssh

Vagrant.configure("2") do |config|
config.ssh.private_key_path = key_path
config.ssh.username = user_name
config.ssh.password = password
config.ssh.forward_agent = True

By setting forward_agent to True, we can use local SSH keys instead of leaving keys sitting on your server.

Using VM — set up a vm

# create an vm# initialize a vm
vagrant init you_folder. # the folder that contain vagrant file
# start a vm
vagrant up
# stop a vm
vagrant stop
# reload vm
vagrant reload

Using VM — using ssh to connect to vm

vagrant ssh

--

--