Vagrant

Post image

Vagrant is a tool for building and managing portable development environments using virtual machines. Here are some things to keep in mind:

  • Available box images can be found on Vagrant Cloud.
  • VirtualBox is just one provider. Vagrant also supports VMware, Hyper-V, Docker, and others.

Basic Commands

# Show the status of all boxes
vagrant status

# Shut down a running box
vagrant halt

init

The init command initializes a new Vagrant environment and creates a Vagrantfile in the current directory.

vagrant init centos/7

up

Starts and provisions the Vagrant box:

vagrant up

ssh

Opens an SSH session into the box. Vagrant handles the SSH keys automatically.

vagrant ssh

snapshot

Saves a snapshot of the current state, allowing you to roll back later:

vagrant snapshot save <option> <vm-name> <name>

boxes

# List all locally installed boxes
vagrant box list

The Vagrantfile

The Vagrantfile defines your virtual machine’s configuration. Here is a typical example:

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
	# Base box image
	config.vm.box = "centos/6"

	# Private network with a static IP
	config.vm.network :private_network, ip: "192.168.58.111"

	# Shared folder between host and guest
	config.vm.synced_folder "../data", '/vagrant_data'

	# Hardware configuration
	config.vm.provider :virtualbox do |vb|
		vb.memory = "1024"
	end

	# Provisioning script that runs on first boot
	config.vm.provision "shell", inline: <<-SHELL
		apt-get update
		apt-get install -y apache2
	SHELL
end

After making changes to the Vagrantfile, apply them with:

vagrant reload

Resources

You May Also Like