Overview:
Vagrant - is an open-source software developed by HashiCorp which enables you to easily create and automate the provisioning process of virtual machines in your local development environments.
Vagrant acts as a wrapper on virtual machines, communicating with them via API providers (hypervisors). You can also code your own API provider. Default provider for Vagrant is - VirtualBox
Vagrant components:
Installation:
To install Vagrant check out the following article: "How to set up Vagrant with Docker on Ubuntu 18.04"
Boxes:
- Box - is a golden image used to boot up the virtual machine
- Bentoo boxes are generalized simple bare-bone boxes that are made in a consistent way
- All available boxes can be found here
- To pull the box image on your system use:
vagrant box add [box_name]
Vagrantfile:
- Vagrantfile describes the overall configuration and provisioning of the environment
- To create Vagrantfile use:
After running the command you should see the following Vagrantfile in your directory:vagrant init
# -*- mode: ruby -*- # vi: set ft=ruby : # All Vagrant configuration is done below. The "2" in Vagrant.configure # configures the configuration version (we support older styles for # backwards compatibility). Please don't change it unless you know what # you're doing. Vagrant.configure("2") do |config| # The most common configuration options are documented and commented below. # For a complete reference, please see the online documentation at # https://docs.vagrantup.com. # Every Vagrant development environment requires a box. You can search for # boxes at https://vagrantcloud.com/search. config.vm.box = "base" # Disable automatic box update checking. If you disable this, then # boxes will only be checked for updates when the user runs # `vagrant box outdated`. This is not recommended. # config.vm.box_check_update = false # Create a forwarded port mapping which allows access to a specific port # within the machine from a port on the host machine. In the example below, # accessing "localhost:8080" will access port 80 on the guest machine. # NOTE: This will enable public access to the opened port # config.vm.network "forwarded_port", guest: 80, host: 8080 # Create a forwarded port mapping which allows access to a specific port # within the machine from a port on the host machine and only allow access # via 127.0.0.1 to disable public access # config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1" # Create a private network, which allows host-only access to the machine # using a specific IP. # config.vm.network "private_network", ip: "192.168.33.10" # Create a public network, which generally matched to bridged network. # Bridged networks make the machine appear as another physical device on # your network. # config.vm.network "public_network" # Share an additional folder to the guest VM. The first argument is # the path on the host to the actual folder. The second argument is # the path on the guest to mount the folder. And the optional third # argument is a set of non-required options. # config.vm.synced_folder "../data", "/vagrant_data" # Provider-specific configuration so you can fine-tune various # backing providers for Vagrant. These expose provider-specific options. # Example for VirtualBox: # # config.vm.provider "virtualbox" do |vb| # # Display the VirtualBox GUI when booting the machine # vb.gui = true # # # Customize the amount of memory on the VM: # vb.memory = "1024" # end # # View the documentation for the provider you are using for more # information on available options. # Enable provisioning with a shell script. Additional provisioners such as # Ansible, Chef, Docker, Puppet and Salt are also available. Please see the # documentation for more information about their specific syntax and use. # config.vm.provision "shell", inline: <<-SHELL # apt-get update # apt-get install -y apache2 # SHELL end
- To set the box in Vagrantfile edit the following line:
config.vm.box = "[box_name]"
- To validate the Vagrantfile use:
vagrant validate
Vagrant up:
- To deploy the Vagranfile use:
vagrant up
- When we run
vagrant up
command:- First, it tries to look for the Vagrantfile in the existing directory where the command was run
- If we run
vagrant up
command in the directory without the actual Vagrantfile, the command will look up the directory tree until it will find the file
- When we run
vagrant up
command it loads a number of other Vagrantfiles to load and merge:- Box Vagrantfile
- User Vagrantfile - specified inside the config directory at
~/.vagrant.d
- Project Vagrantfile
- Multi-machine
- Provider
Example deployment of Ubuntu server:
- Create folder to store your Vagrantfile and other config files:
mkdir ubuntu-server
- Automatically create the template Vagrantfile:
vagrant init
- Edit Vagrantfile as follows:
# -*- mode: ruby -*- # vi: set ft=ruby : # # All Vagrant configuration is done below. The "2" in Vagrant.configure # configures the configuration version (we support older styles for # backwards compatibility). Please don't change it unless you know what # you're doing. Vagrant.configure("2") do |config| # The most common configuration options are documented and commented below. # For a complete reference, please see the online documentation at # https://docs.vagrantup.com. # # Every Vagrant development environment requires a box. You can search for # boxes at https://vagrantcloud.com/search. config.vm.box = "hashicorp/bionic64" # # Disable automatic box update checking. If you disable this, then # boxes will only be checked for updates when the user runs # `vagrant box outdated`. This is not recommended. # config.vm.box_check_update = false # # Create a forwarded port mapping which allows access to a specific port # within the machine from a port on the host machine. In the example below, # accessing "localhost:8080" will access port 80 on the guest machine. # NOTE: This will enable public access to the opened port # config.vm.network "forwarded_port", guest: 80, host: 8080 # # Create a forwarded port mapping which allows access to a specific port # within the machine from a port on the host machine and only allow access # via 127.0.0.1 to disable public access # config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1" # # Create a private network, which allows host-only access to the machine # using a specific IP. # config.vm.network "private_network", ip: "192.168.33.10" # # Create a public network, which generally matched to bridged network. # Bridged networks make the machine appear as another physical device on # your network. config.vm.network "public_network" # # Share an additional folder to the guest VM. The first argument is # the path on the host to the actual folder. The second argument is # the path on the guest to mount the folder. And the optional third # argument is a set of non-required options. # config.vm.synced_folder "../data", "/vagrant_data" # # Provider-specific configuration so you can fine-tune various # backing providers for Vagrant. These expose provider-specific options. # Example for VirtualBox: # # config.vm.provider "virtualbox" do |vb| # # Display the VirtualBox GUI when booting the machine # vb.gui = true # # # Customize the amount of memory on the VM: # vb.memory = "1024" # end # # View the documentation for the provider you are using for more # information on available options. # # Enable provisioning with a shell script. Additional provisioners such as # Ansible, Chef, Docker, Puppet and Salt are also available. Please see the # documentation for more information about their specific syntax and use. # config.vm.provision "shell", inline: <<-SHELL # apt-get update # apt-get install -y apache2 # SHELL end
config.vm.network "public_network"
- enables a bridged network by connecting your VM to your host machine. This allows Internet access for VM- If you want to change your provider add the following config line:
Example of enabling HyperV for Windows based VMs:config.vm.provider "[provider_name]"
config.vm.synced_folder ".", "/vagrant_data", disabled: true config.vm.provider "hyperv" config.vm.provider "hyperv" do |h| h.enable_virtualization_extensions = false h.linked_clone = true end
- Deploy the machine:
During the deployment Vagrant will download the box image and automatically create SSH keys to log into the machinevagrant up
- To log into the machine type:
You should see the following message:vagrant ssh
- After adding new changes to the Vagrantfile, to apply the changes use:
For example, to change the name of the machine for VirtualBox provider use:vagrant reload
config.vm.provider "virtualbox" do |vb| # Set VirtualBox machine name vb.name = "ubuntu_server_18.04" end
vagrant reload
command stops VM, apply the changes and start the machine again
Tips:
- To list the status of current running VM use:
vagrant status
- To enable the feature for listing VMs you can install a third-party plugin https://github.com/joshmcarthur/vagrant-list
To install the plugin use:
To list existing VMs:vagrant plugin install vagrant-list
vagrant list
Commands:
- Initialize Vagrantfile and config files:
vagrant init
- Deploy VM:
vagrant up
- Validate Vagrantfile for any syntax errors:
vagrant validate
- List downloaded boxes on your host machine:
vagrant box list
- To add the box:
vagrant box add [box_name]
- To remove the box:
vagrant box remove [box_name]
- To check the status of running machine:
vagrant status
- To log into the machine:
vagrant ssh
- To apply the changes to Vagrantfile use:
vagrant reload
- To get list of all currently running machines use:
vagrant global-status
- To prune any invalid cache entries in order to get fresh info use:
vagrant global-status --prune
- To package running VM into box package use:
vagrant package
- To stop the machine use:
To resume stopped machine use:vagrant halt
vagrant up
- To get debug info on deployment use:
vagrant --debug up
- To display full list of guest ports mapped to the host machine ports:
vagrant port [vm_name]
- To delete the machine use:
vagrant destroy