Chapter -3 | About Vagrant Installation and Setup & Vagrant Cheat Sheet
Vagrant is the command line utility for managing the virtual machines
What is Vagrant?
Vagrant is a tool for building and managing virtual machine environments in a single workflow. It makes life easier by lowering development environment setup time and increasing productivity.
Vagrant is the command line utility for managing the virtual machines. The following image shows the overview of vagrant.
Now let us understand this using a diagram
Managing various VM and configuring it is quite tedious. So using vagrant we can able to manage, configure and run multiple VM using a file known as “Vagrant File”.
A Vagrant file can keep the configuration of the VM such as OS, RAM, storage, and so on. when we run vagrant up command in a terminal, vagrant reads through the vagrant file and starts executing and creating VM according to the configuration.
Features of Vagrant
Vagrantfile:
- A Vagrantfile is a configuration file for controlling virtual machine.
- It uses simple ruby syntax Refer here
- It contains all the necessary information needed for other user who wants to recreate the same VM.
- If the user needs to recreate the virtual machine created, then only Vagrantfile is shared
Boxes
- Boxes are the package format for Vagrant
- Boxes can be easily downloaded by using
vagrant box add
command - Publicly available boxes are shared here.
- Every Box is restricted to run on supported providers. So while choosing the box checkwhether it matches your Provider(VirtualBox/Hyper-V/VmWare)
Networking
Vagrant supports 3 main types of networking
- Public Networking
- Private Networking
Port - Forwarding
Port-forwarding is the action of linking a port on your host machine to a port on the guest machine. Refer Link
- Private Networking allows the virtual machine to be accessed via private address. Refer Link
- Public Networking attempts to allow public access from outside your host machine (if your provider and machine will allow it) instead of just allowing access from inside the host machine. Refer Link
Provisioning
- Provisioning offers you the way to configure the virtual machine by executing Scripts or configurations. Refer here Provisioning happens in the following cases
- Executing
vagrant up
command for the first time - Executing
vagrant provision
command - Executing vagrant
reload --provision
command
Plugins
- To change the way Vagrant does something or add additional functionality to Vagrant. this can be done via Vagrant plugins.
Creating a VM
vagrant init
Initialize Vagrant with a Vagrantfile and ./.vagrant directory, using no specified base image. Before you can do vagrant up, you'll need to specify a base image in the Vagrantfile.vagrant init <boxpath>
Initialize Vagrant with a specific box.
Starting a VM
vagrant up
starts vagrant environment (also provisions only on the FIRST vagrant up)vagrant resume
resume a suspended machine (vagrant up works just fine for this as well)vagrant provision
forces reprovisioning of the vagrant machinevagrant reload
restarts vagrant machine, loads new Vagrantfile configurationvagrant reload provision
restart the virtual machine and force provisioning
Getting into a VM
vagrant ssh
connects to machine via SSHvagrant ssh <boxname>
If you give your box a name in your Vagrantfile, you can ssh into it with boxname. Works from any directory.
Stopping a VM
vagrant halt
stops the vagrant machinevagrant suspend
suspends a virtual machine (remembers state)
Cleaning up a VM
vagrant destroy
stops and deletes all traces of the vagrant machinevagrant destroy -f
same as above, without confirmation
Boxes
vagrant box list
see a list of all installed boxes on your computervagrant box add <name> <url>
download a box image to your computervagrant box outdated
check for updates vagrant box updatevagrant box remove <name>
deletes a box from the machinevagrant package
packages a running virtualbox env in a reusable box
Saving Progress
vagrant snapshot save [options] [vm-name] <name>
vm-name is often default. Allows us to save so that we can rollback at a later time
Tips
vagrant -v
get the vagrant versionvagrant status
outputs status of the vagrant machinevagrant global-status
outputs status of all vagrant machinesvagrant global-status --prune
same as above, but prunes invalid entriesvagrant provision --debug
use the debug flag to increase the verbosity of the outputvagrant push
yes, vagrant can be configured to deploy code!vagrant up --provision | tee provision.log
Runs vagrant up, forces provisioning and logs all output to a file
Plugins
vagrant-hostsupdater : $ vagrant plugin install vagrant-hostsupdater
to update your/etc/hosts
file automatically each time you start/stop your vagrant box.