Vagrant - Getting Started
15 Jan 2016Vagrant is a tool for managing virtual machines. It provides easy to configure, reproducible and portable work environments.
Machines are provisioned on top of :
- VirtualBox
- VMWare
- AWS etc.
Vagrant helps to isolate dependencies and their configuration within a single disposable, consistent environment.
Up and Running
mkdir vagrant_getting_started
cd vagrant_getting_started
#Create Vagrant File
vagrant init <boxname>
#Start Vagrant Box
vagrant up
#Connecting to Vagrant Box
vagrant ssh
Using Boxes
Vagrant.configure("2") do |config|
config.vm.box = "hashicorp/precise32"
end
List of Boxes - https://atlas.hashicorp.com/boxes/search
Provisioning with Vagrant
Vagrant has built-in support for automated provision. Using this feature, it allows to automatically install software when you do vagrant up
You can also provision using vagrant provision
Below example will run a bootstrap.sh script file during the provision process.
Vagrant.configure("2") do |config|
config.vm.box = "hashicorp/precise32"
config.vm.provision :shell, path: "bootstrap.sh"
end
#!/bin/bash
echo "Adding Necessary Stuff"
sudo apt-get update
sudo apt-get upgrade
sudo apt-get -y install build-essential
sudo apt-get -y install nodejs npm node-semver
sudo apt-get -y install git
sudo apt-get -y install openjdk-7-jdk
Networking with Vagrant
Vagrant.configure("2") do |config|
config.vm.box = "hashicorp/precise32"
config.vm.provision :shell, path: "bootstrap.sh"
config.vm.network :forwarded_port, guest: 80, host: 4567
config.vm.network :forwarded_port, guest: 5000, host: 5000
end
TearDown
vagrant suspend
vagrant halt
vagrant destroy
Packaging a Customized Vagrant Box
vagrant package --vagrantfile Vagrantfile.pkg --output artBox.box
vagrant box add mynewbox mynew.box
mkdir newBox
cd newBox
vagrant init mynewbox