Virtual machines, quickly. Requires a hypervisor, such are virtualbox to start with.
Online resource is excellent: https://www.vagrantup.com/docs/
$ vagrant init hashicorp/precise64 $ vagrant up
vagrant init
Vagrant.configure("2") do |config|
config.vm.box = "hashicorp/precise64"
config.vm.box_version = "1.1.0"
config.vm.box_url = "http://files.vagrantup.com/precise64.box"
end
$ vagrant ssh
From within the vagrant vm, the host directory can be accessed from /vagrant
Suppose after bringing up the guest, certain routine tasks need to be performed, such as installing a package. This can be achieved through provisioning. Just capture the tasks in a script, e.g. bootstrap.sh and add the following in Vagrantfile.
Vagrant.configure("2") do |config|
config.vm.box = "hashicorp/precise64"
config.vm.provision :shell, path: "bootstrap.sh"
end
The "provision" line is new, and tells Vagrant to use the shell provisioner to setup the machine, with the bootstrap.sh file. The file path is relative to the location of the project root (where the Vagrantfile is).
To apply this on a running vagrant, execute:
$ vagrant reload --provision
Typically, ports from within the vagrant guest are not visible in the host. Using port forwarding to enable access to them:
Vagrant.configure("2") do |config|
config.vm.box = "hashicorp/precise64"
config.vm.provision :shell, path: "bootstrap.sh"
config.vm.network :forwarded_port, guest: 80, host: 4567
end
To enable bridged networking on interface eth0 using a custom MAC use the following:
config.vm.network "public_network", :mac => "000000000003", bridge: "eth0"
And the following is required for the VM to get internet access:
config.vm.provider "virtualbox" do |v|
v.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
v.customize ["modifyvm", :id, "--natdnsproxy1", "on"]
end
$ vagrant login $ vagrant share