Vagrant in 5 Minutes
I have used vagrant since quite long time. With the recent devops movement, I want to revisit my understanding of the tool because it is very useful to use vagrant when you are simulating distributed system in your local machine. Of course the simulation wouldn't be 100% accurate but it is simpler to do than if you have to spawn some new VM machines in the cloud.
Instalation
This installation guide is for Mac OS. Although vagrant is not a GUI apps, but it is only available under homebrew/cask
formula.
$ brew install homebrew/cask/vagrant
It is also possible to install using binary but I still prefer to use brew.
If you don't have virtualisation software then we also need to install one. The easy choice will be virtualbox.
$ brew install homebrew/cask/virtualbox
Check the installation with:
$ vagrant --version
$ VBoxManage --version
Running
To run a VM using vagrant we need to define the VM spec inside Vagrantfile. vagrant provides a helper to generate the file filled with documentation without the need for us to write it from scratch.
$ vagrant init ubuntu/bionic64
The argument after init is the VM box that we want to use, which is for above case is Ubuntu 18.08 Bionic Beaver.
It will add this configuration line in the Vagrantfile:
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/bionic64"
end
$ vagrant up
After running up, vagrant will try to download the box if it is not available in local machine. Then it will startup the VM operating system.
Run this to check the status of the VM:
$ vagrant status
Run this to get in to the machine:
$ vagrant ssh
Run these to stop or suspend the machine:
$ vagrant halt # stop the machine
$ vagrant suspend
Networking
Vagrant support different kind of networking setup. To test the setup, after updating the Vagrantfile, run reload to apply the updated config.
$ vagrant reload
Forward Port Mapping
This configuration setup will forward the call from your machine port 8080 to port 80 in our ubuntu/bionic64. Please note that with this config, the port forwarding is also accessible from other computers in your network.
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/bionic64"
config.vm.network "forwarded_port", guest: 80, host: 8080
end
Forward Port Mapping Disable Public Access
Similar like above, but the access now is limitted only from your machine.
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/bionic64"
config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"
end
Private Network Disable Public Access
Instead of forwarding port, with private network the guest OS will have its own ip address.
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/bionic64"
config.vm.network "private_network", ip: "192.168.33.10"
end
Public Network
With this config the vagrant machine will appear like another physical machine in your network.
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/bionic64"
config.vm.network "public_network"
end
Folder Sharing
We can share files between host machine and vagrant guest OS using this config.
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/bionic64"
config.vm.synced_folder "data_from_my_laptop", "/vagrant_data"
end
To test it just create some files in your machine.
$ mkdir data_from_my_laptop
$ touch data_from_my_laptop/foo
$ echo "hello world" > data_from_my_laptop/bar
$ vagrant reload
$ vagrant ssh
vagrant@ubuntu-bionic $ ls /vagrant_data
Network and folder sharing setup should cover most common use case of using vagrant. For more details please refer to vagrant official documentation.