Vagrant Basics

Ayush Goyal

Vagrant

A tool to manage virtual machines. Its used to creating reproducible working environments for testing and development

Virtual Machine

Virtual machine is software for emulation of a computer system.

  • There are chiefly 2 categories of virtual machines, System or Process
  • Today we are concerned with System Virtual Machine
    • A system virtual machine can emulate hardware of a physical computer
    • They are used for isolating working/execution environments. This strategy is used to provide service in cloud.
    • Its also useful for testing for systems/architecture for which you don't have physical hardware.

Popular Virtual Machine software

  • VirtualBox
  • KVM
  • xen
  • VMware

Demo

Setting up vagrant on mac

            brew tap caskroom/cask
            brew cask install virtualbox
            brew cask install vagrant

Adding a vagrant box

Boxes are the package format for Vagrant environments. A box can be used by anyone on any platform that Vagrant supports to bring up an identical working environment.

  • Copy the "ubuntu_12.04_java7.box" file from bagoba to your home folder
  • Add the copied image to vagrant using following command

    vagrant box add ubuntu-12.04 ubuntu_12.04_java7.box
    

Initialize Vagrant box

  • Create a folder for your development environment

    mkdir testvagrant
    cd testvagrant
    
  • Initialize Vagrant with the box you just imported

    vagrant init ubuntu-12.04
    
  • Notice that there is a `Vagrantfile` in your dir now. Open it in your text editor

Start vagrant machine

  • Start vm with:

    vagrant up
    
  • SSH into your vagrant machine:

    vagrant ssh
    

Sharing Files with Vagrant

  • By default, vagrant mounts the directory you spawn your machine from at /vagrant inside the vm
  • One advantage of sharing files like this is that you can use your favorite tools to code and then execute it from within VM

Provisioning on your vagrant machine

  • Add these lines in your Vagrantfile. Uncomment if they are already present:

    config.vm.provision "shell", inline: <-SHELL
      sudo apt-get update
      sudo apt-get install -y apache2
    SHELL
    
  • Provision your vagrant machine:

    vagrant provision
    

Configure your vagrant machine with a accessible private static ip

  • Provision your vagrant machine:

    config.vm.network "private_network", ip: "192.168.33.10"
    
  • Restart vagrant using:

    vagrant reload
    

Suspend, Stopping and destroying vagrant machine

  • Suspend your vagrant machine:

    vagrant suspend
    
  • Stop your vagrant machine:

    vagrant halt
    
  • Destroy your vagrant machine:

    vagrant destroy
    

Vagrant plug-ins

Vagrant's functions can be extended via plugins. Lots of amazing plug-ins available via community. Some Useful plugins:

  • vagrant-cachier: helps with fetching packages again and again using a NFS cache volume
  • vagrant-scp: copy files in vagrant using scp

Vagrant Basics

By Ayush Goyal

Vagrant Basics

  • 1,989