Using Vagrant
Here are a few observations/hints about Vagrant if you have not used it before. Vagrant is pretty complex since it’s targeted at real-world devops usage and is not a teaching-focused tool. It’s super useful though, especially for creating non-trivial reproducible environments with code (the vagrantfile + provisioning scripts) that you can share between developers.
The lifecycle of a machine in Vagrant goes through the following phases:
- if needed, the vm instance is allocated
- The allocated instance is started,
- The instance runs
- If needed, the instance is provisioned
- The instance is ready to use!
5.5 [optional]) The instance can be suspended and awoken
- The instance is stopped
- The instance is destroyed and deallocated
The vagrant up
command is designed to do whatever it necessary to get the VMs
to the ready to use state from its current state. The first time you run vagrant
up, it does the first 5 steps, including allocating a new vm and provisioning
it. If you immediately run it again, it actually will not “do” anything, since
the vm is already in the ready state!
You may have already discovered the suspend
, halt
, and destroy
commands.
These all operate at different levels of “severity” in stopping the vm.
If you do suspend
, the VM state is saved to disk and can be restored later to
exactly where you left it (with up
). This corresponds to “closing the lid of
your laptop”. All the state is still saved, but the machine is hibernating.
Later doing up will restore the state, and is like re-opening your laptop lid.
If you do halt
, the vm is actually stopped and shut down. The disk image still
exists, but the current system state is stopped. This is the equivalent to
shutting down your computer. reload
combines a halt with an immediate up,
corresponding to a restart.
If you do destroy
, the vm is stopped, and its persistent disk image is
destroyed. This corresponds to throwing your laptop out the window, never to
see it again. After vagrant destroy
, vagrant up
has to start all the way
from step 1 again, and will allocate a new instance and provision it for you.
For this project, you probably want to mostly be using up
and suspend
until
you’re done, unless you get something in a bad state and want to start with a
clean VM!
The one “weird” lifecycle command is provision
. Provision corresponds to
running a script to reinstall all your favorite applications. Vagrant tries to
only do provisioning with a completely fresh VM image, but you can force it to
provision again with vagrant up --provision
or just vagrant provision
once a
machine is already up. Once provisioning completes successfully for a given VM
instance (installing all your favorite apps and games on your laptop), you don’t
need to run it again unless you destroy the instance.
You can get more details about these commands and vagrant itself via their official docs, or ask questions in ed!