anitsh / til

Today I Learn (til) - Github `Issues` used as daily learning management system for taking notes and storing resource links.
https://anitshrestha.com.np
MIT License
76 stars 11 forks source link

Vagrant CLI #628

Open anitsh opened 2 years ago

anitsh commented 2 years ago
command description
vagrant box add ORG/BUILD Add a new virtual machine
vagrant init ORG/BUILD Initialize virtual machine
vagrant up Start up virtual machine
vagrant reload Restart virtual machine
vagrant halt Shut down virtual machine
vagrant ssh SSH into the virtual machine

Creating a VM

vagrant init -- Initialize Vagrant with a Vagrantfile and ./.vagrant directory, using no specified base image. Before you can do vagrant up, you'll need to specify a base image in the Vagrantfile.
vagrant init <boxpath> -- Initialize Vagrant with a specific box. To find a box, go to the public Vagrant box catalog. When you find one you like, just replace it's name with boxpath. For example, vagrant init ubuntu/trusty64.

Starting a VM

vagrant up -- starts vagrant environment (also provisions only on the FIRST vagrant up)
vagrant resume -- resume a suspended machine (vagrant up works just fine for this as well)
vagrant provision -- forces reprovisioning of the vagrant machine
vagrant reload -- restarts vagrant machine, loads new Vagrantfile configuration
vagrant reload --provision -- restart the virtual machine and force provisioning

Getting into a VM

vagrant ssh -- connects to machine via SSH
vagrant ssh <boxname> -- If you give your box a name in your Vagrantfile, you can ssh into it with boxname. Works from any directory.

Stopping a VM

vagrant halt -- stops the vagrant machine
vagrant suspend -- suspends a virtual machine (remembers state)

Cleaning Up a VM

vagrant destroy -- stops and deletes all traces of the vagrant machine
vagrant destroy -f -- same as above, without confirmation

Boxes

vagrant box list -- see a list of all installed boxes on your computer
vagrant box add <name> <url> -- download a box image to your computer
vagrant box outdated -- check for updates vagrant box update
vagrant boxes remove <name> -- deletes a box from the machine
vagrant package -- packages a running virtualbox env in a reusable box

Saving Progress

-vagrant snapshot save [options] [vm-name] -- vm-name is often default. Allows us to save so that we can rollback at a later time Tips

vagrant -v -- get the vagrant version
vagrant status -- outputs status of the vagrant machine
vagrant global-status -- outputs status of all vagrant machines
vagrant global-status --prune -- same as above, but prunes invalid entries
vagrant provision --debug -- use the debug flag to increase the verbosity of the output
vagrant push -- yes, vagrant can be configured to deploy code!
vagrant up --provision | tee provision.log -- Runs vagrant up, forces provisioning and logs all output to a file

Plugins

vagrant-hostsupdater : $ vagrant plugin install vagrant-hostsupdater to update your /etc/hosts file automatically each time you start/stop your vagrant box.
anitsh commented 2 years ago

Port forward

Another way to set up a temporary terminal, besides opening up the VirtualBox settings, is to use vagrant ssh, with additional arguments given after the --:

vagrant ssh -- -L 3000:localhost:3000

This will forward port 3000 on the host to port 3000 on the guest.

The first number is for the host. To forward port 7001 on the host to the default postgresql port on the guest:

vagrant ssh -- -L 7001:localhost:5432

This will only last as long as your ssh session. If ssh gets disconnected, run it again. To make it persist after restarts, add it to your Vagrantfile.

Must specified id: "ssh" in order to override the default forwarded SSH port. config.vm.network :forwarded_port, guest: 22, host: 2322, id: "ssh"

Resource