- Creating Development Environments with Vagrant(Second Edition)
- Michael Peacock
- 766字
- 2021-07-23 20:04:22
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:
- 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).
- Create a new virtual machine with the relevant provider (the default being VirtualBox).
- Forward any configured ports; by default, it will forward port
22
(the SSH port) on the virtual machine to port2222
on the host. This will allow us to connect to the virtual machine over SSH. - Boot (power up) the virtual machine.
- Configure and enable networking, so that we can communicate with the virtual machine.
- 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). - 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:

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:

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

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.

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:

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).
- Visual Studio 2012 Cookbook
- JavaScript+DHTML語法與范例詳解詞典
- 圖解Java數據結構與算法(微課視頻版)
- Android Studio Essentials
- PLC編程及應用實戰
- MATLAB定量決策五大類問題
- Getting Started with Python Data Analysis
- Create React App 2 Quick Start Guide
- Instant PHP Web Scraping
- Java Web應用開發項目教程
- 貫通Tomcat開發
- Delphi開發典型模塊大全(修訂版)
- Developer,Advocate!
- The Applied Data Science Workshop
- 軟件再工程:優化現有軟件系統的方法與最佳實踐