Creating Development Environments with Vagrant(Second Edition)
上QQ阅读APP看书,第一时间看更新

Managing Vagrant-controlled guest machines

The virtual machines, which Vagrant controls for us, still need to be managed and worked with. We have seen that we can start a Vagrant project with vagrant up. Let's learn more about this command, and see how to perform other operations on our new virtual machine.

Powering up a Vagrant-controlled virtual machine

As we have just seen, we can power up a virtual machine using the vagrant up command. With this command, Vagrant will first check whether a Vagrant environment has already been set up. If a previously suspended environment is found, it will resume that environment.

If the environment was not previously suspended, Vagrant then checks whether the base box has already been downloaded onto the machine. If it hasn't, it will download it, as it did for us when we booted our project with the precise64 box for the first time.

Vagrant will then perform the following actions:

  1. Copy the base box (remember, base boxes are managed globally on our host computer, so it takes a copy for each machine managed by it).
  2. Create a new virtual machine with the relevant provider (the default being VirtualBox).
  3. Forward any configured ports; by default, it will forward port 22 (the SSH port) on the virtual machine to port 2222 on the host. This will allow us to connect to the virtual machine over SSH.
  4. Boot (power up) the virtual machine.
  5. Configure and enable networking, so that we can communicate with the virtual machine.
  6. Map shared folders between the host and the guest (by default, it will map the folder that contains the Vagrant project to /vagrant on the guest machine).
  7. Run any provisioning tools that are set up such as Puppet, Chef, or SSH commands or scripts.

The actions performed by Vagrant will look something like this:

Powering up a Vagrant-controlled virtual machine

Suspending a virtual machine

We can save the current state of the virtual machine to the disk (suspend it) so that we can resume it later. If we run vagrant suspend, it will suspend the VM and stop it from consuming our machine's resources, except for the disk space it will occupy, ready for us to use again later:

Suspending a virtual machine

Resuming a virtual machine

In order to use a previously suspended virtual machine, we simply run vagrant resume:

Resuming a virtual machine

Shutting down a virtual machine

We can shut down a running virtual machine using the vagrant halt command. This instructs the VM to stop all running processes and shut down. To use it again, we need to run vagrant up, which will power on the machine; provisioning is typically only ran on the first vagrant up command. To ensure that provisioning runs when we boot up a saved machine subsequently, we can use the --provision flag.

Shutting down a virtual machine

Starting from scratch

Sometimes, things go wrong. It's not inconceivable that we might make some changes to our virtual machine, and find out that it no longer works. Thankfully, since we have a base box, configuration file, and provisioning files, which are all stored separately, we can instruct Vagrant to destroy our virtual machine, and then create it again, using the configurations to set it up. This is done via the destroy command, and then we need to use the up command to start it again:

vagrant destroy
vagrant up

Of course, if we update our Vagrantfile, provisioning manifests, or application code that can also break things; so it is important that we use a version control system to properly manage our project's code and configuration, so that we can undo the changes there too; Vagrant can only do so much to help us!

Updating based on Vagrantfile changes

If we make changes to our Vagrantfile, these changes won't apply until we next shut down and power on our virtual machine. As this isn't very convenient, there is a handy reload command that will shut down the machine, reload its configuration based on the Vagrantfile as it currently is, and boot it up again:

vagrant reload

Running this command yields the following result:

Updating based on Vagrantfile changes

Connecting to the virtual machine over SSH

If we run the vagrant ssh command, Vagrant will then connect to the virtual machine over SSH. Alternatively, we can SSH to localhost with port 2222, and this will tunnel into the virtual machine, using the default forwarded SSH port.

If we run Vagrant on a Windows machine, we won't have a built-in SSH client. We can use a client such as PuTTY to connect to Vagrant. PuTTY can be downloaded from http://www.chiark.greenend.org.uk/~sgtatham/putty/. More information on how to configure PuTTY to work with Vagrant is available on the Vagrant website (http://docs-v1.vagrantup.com/v1/docs/getting-started/ssh.html).