Vagrant

vagrantup.com

🌐 Resources πŸ”—

  • Vagrant Installationarrow-up-right

    • Downloadarrow-up-right and Run the installer for Windows system.

    • The installer will automatically add vagrant to your system path so that it is available in terminals.

    • Verify the correct installation using the vagrant command inside PowerShell.

  • The primary function of the Vagrantfilearrow-up-right is to describe the type of machine required for a project, and how to configure and provision these machines. Vagrantfiles are called Vagrantfiles because the actual literal filename for the file is Vagrantfile.

  • Vagrant is meant to run with one Vagrantfile per project.

  • When you run any vagrant command, Vagrant climbs up the directory tree looking for the first Vagrantfile it can find, starting first in the current directory.

    • This feature lets you run vagrant from any directory in your project.


Default Provider

  • If you have the VMware provider installed, it will always take priority over VirtualBox.

  • Check more infos about Default Providers herearrow-up-right

  • E.g.


VirtualBox

  • Install VirtualBoxarrow-up-right & VirtualBox Extension Pack (default Vagrant provider)

  • Create a new directory for the vagrant project

  • Open PowerShell and move into the project directory

  • Initialize the directory with a box, in this case the hashicorp/bionic64arrow-up-right box will be used

  • box add subcommand can be used to install a box without creating a new Vagrantfile

    • do not add it if already initialized

This will download the box named hashicorp/bionic64 from HashiCorp's Vagrant Cloud box catalogarrow-up-right, where you can find and host boxes.

Boxes are globally stored for the current user. Each project uses a box as an initial image to clone from, and never modifies the actual base image. This means that if you have two projects both using the hashicorp/bionic64 box you just added, adding files in one guest machine will have no effect on the other machine.

  • Use the box as a base in the project by opening the Vagrantfile. Content must be:

The hashicorp/bionic64 in this case must match the name you used to add the box above. This is how Vagrant knows what box to use. If the box was not added before, Vagrant will automatically download and add the box when it is run.

  • Bring up the virtual machine

vagrant up --provider=virtualbox
  • VirtualBox - created and started VM

  • SSH into the machine:

  • Destroy the machine

  • Remove the box

  • Extras:

    • Suspend the VM

      • The virtual machine will still use disk space while suspended, and requires additional disk space to store the state of the virtual machine RAM.

    • Gracefully shutdown the VM

      • Halting your machine will cleanly shut it down, preserving the contents of disk and allowing you to cleanly start it again.


VMware Workstation

  • Create a new directory for the vagrant project

  • Open PowerShell and move into the project directory

  • Initialize the directory with a box, in this case the hashicorp/bionic64arrow-up-right box will be used

  • Bring up the virtual machine and try to SSH into it

vagrant up --provider=vmware_desktop
  • VMware Workstation - created and started VM

  • Shutdown the VM and destroy it

πŸ“Œ

  • After stopping the VM with vagrant halt, at the second boot with vagrant up this error could appear ❗:

Vagrant encountered an error while attempting to prune unused port forward entries

  • To solve this port forwarding issue use this command:

  • Then bring up again the VM with vagrant up --provider=vmware_desktop command.


πŸ“ Cheatsheet


Last updated