Vagrant is a tool that allows the user to spawn, launch, halt and manage multiple Virtual Machines easily. It's a CLI to various backends (so far only Virtualbox and VMWare). Here’s a list of commands that would help any newcomer to Vagrant to be productive quickly. This is not meant to be exhaustive. For (a lot) more, check out
the official documentation.
Boxes
Boxes are images Vagrant uses to create VMs. You can look for boxes online or pass them around on the network. From the doc:
Boxes are the skeleton from which Vagrant machines are constructed. They are portable files which can be used by others on any platform that runs Vagrant to bring up a working environment.
To add a box
You can find some boxes on
Vagrantbox.es
$ vagrant box add mynewbox http://url/to/some/box
Or a box that you have locally
$ vagrant box add mynewbox /path/to/some/box
To list the existing boxes
$ vagrant box list
Vagrantfiles
A Vagrantfile describes the network of machines you are building. When executing a command, vagrant will look for a file named “Vagrantfile” in the current directory. You can learn how to write complex Vagrantfiles in the official documentation. Here’s the simplest example:
Init file
$ vagrant init
$ ls
Vagrantfile
This command has created a minimal Vagrantfile. Modify it so that it includes your selected box:
Vagrant.configure("2") do |config|
config.vm.box = "base"
end
This configuration defines one machine based on the box "base" (the machine will be called “default”). It will use default values for everything else.
Machines status
$ vagrant status
Current VM states:
default not created
The environment has not yet been created. Run `vagrant up` to
create the environment.
Other statuses include:
Status is particularly useful when you have several machines in a Vagrantfile.
Install and run
$ vagrant up
$ vagrant status
Current VM states:
default running
This command will start the VM. If the VM hasn’t been created or has been destroyed, it will first install the OS on the machine.
Connect to the machine
$ vagrant ssh
ssh is activated by default.
- Username: vagrant
- Password: vagrant
- sudo: enabled
File sharing
By default, Vagrant automatically maps the current directory to the /vagrant directory in the guest machine. Files are instantly synchronized.
Halt a machine
$ vagrant halt
Use this command to put a machine in the ‘poweroff’ status. It will boot into the exact same state next time you run ‘vagrant up’
Destroy a machine
$ vagrant destroy
This command will completely destroy the machine.
Multiple machines
Vagrantfiles
It is possible to define more than one machine in a Vagrantfile. Like this:
Vagrant.configure("2") do |env_config|
env_config.vm.define "db1" do |config|
config.vm.box = "ub1204_64"
config.vm.network :private_network, ip: "192.168.1.1"
end
env_config.vm.define "db2" do |config|
config.vm.box = "ub1204_64"
config.vm.network :private_network, ip: "192.168.1.2"
end
env_config.vm.define "webserver" do |config|
config.vm.box = "ub1204_64_firewalled"
config.vm.network :private_network, ip: "192.168.2.0"
end
end
vagrant commands
When you have more than one machine defined in your Vagrantfile, certain commands like “vagrant ssh” are ambiguous. Two things to keep in mind:
Append one or more machine names
$ vagrant up db1 db2 webserver
No machine name will mean all (when applicable): “vagrant up” will execute for every machine defined, but “vagrant ssh” will throw an error.
Networking
10.0.2.15
By default, vagrant will create an interface eth0 in each machine and assign it IP 10.0.2.15. The virtual machine uses this interface for communication with vagrant. I find it easier to avoid using this interface for hosting my services.
Add an interface
Here’s how we can define an aditional interface on a machine:
Vagrant.configure("2") do |config|
config.vm.box = "debian_wheezy"
config.vm.network :hostonly, ip: "192.168.33.10"
end
This has defined a host-only interface. This is taken from
the official Virtualbox documentation:
Host-only networking
This can be used to create a network containing the host and a set of virtual machines, without the need for the host’s physical network interface. Instead, a virtual network interface (similar to a loopback interface) is created on the host, providing connectivity among virtual machines and the host.