- Creating Development Environments with Vagrant(Second Edition)
- Michael Peacock
- 800字
- 2025-02-19 14:05:20
Managing integration between host and guest machines
Without any form of integration between the host machine and the guest, we would simply have a bare bones virtual server running on top of our own operating system, which is not particularly useful. We need our own machine to be capable of integrating tightly with the guest (virtual machine).
Port forwarding
Although the virtual machine is running on our own machine, because of virtualization, it acts and behaves like a completely different machine. Sometimes, this is what we want; however, there might be times we want to have the virtual machine behave almost as an extension of our own machine. One way to do this is through port forwarding, where we can tunnel a port from the virtual machine to a port on the host machine. If, for example, we have a web server running on our own machine, we obviously don't want to map the web server port from Vagrant onto the same port; otherwise, there would be a conflict. Instead, we can map it to another port. If we map the web server port on the virtual machine to port 8888
on the host, then navigating to http://localhost:8888
on our own machine would show us the web service we run on the guest, despite the fact that the localhost refers to our host machine.
The port forwarding is done via lines in the Vagrantfile
file; we simply provide the guest and host ports we wish to map:
config.vm.network :forwarded_port, guest: 80, host: 8888
If we have other Vagrant managed virtual machines on our computer, which we wish to run simultaneously, we can enable auto_correct
on specific ports. This way, if a conflict is found (for example, two virtual machines trying to map to the same port), one virtual machine will try a different port instead:
, auto_correct: true
Ports below a certain range need elevated or root privileges on the host machine, so you may be asked for your administrative password.
Synced folders
Synced folders allow us to share a folder between the host and the guest. By default, Vagrant shares the folder that contains the Vagrant project /vagrant
on the virtual machine. We can use the following command in our Vagrantfile
to sync more folders if we wish:
config.vm.synced_folder "/Users/michael/assets/" "/var/www/assets"
The first parameter is the path to the folder on our machine and the second is the mount point on the VM. If we use a relative path on our machine, it would be relative to the project folder.
If we want to override the default synced folder, we can do this too:
config.vm.synced_folder ".", "/var/another/folder"
The Network File System (NFS) can give us better performance with synced folders than the default settings. This won't have any effect on Windows hosts, and on Linux/OS X, hosts will require root privileges. We can enable NFS on a per synced folder basis by adding the following command to the preceding line:
, type: "nfs"
Networking
By default, our Vagrant virtual machines are only accessible from the machines we run Vagrant on, and other machines in our network won't be able to access them. If we map ports to our host, then we can share the services running on the virtual machine with our colleagues within our network. If we want to allow our colleagues to access our Vagrant managed virtual machines directly, we can attach the virtual machine to our internal network, and VirtualBox will bridge the network between our machine and the virtual machine, and the internal network between our machine and the rest of the machines in our home or office.
config.vm.network "private_network", ip: "10.11.100.200"
This approach is also useful when wanting to have multiple Vagrant projects running at the same time; if they are web projects, they can all expose port 80
, but on different IP addresses, and if we want, we can map these to the hostnames in our hosts
file.
Note
The hosts
file is a file on a computer that maps a domain name to an IP address. This can be used to prevent the computer from having to look up the IP address for a domain and is useful for locally hosted sites, as we can manually link the IP address to a domain name, just for our local machine. On OS X and Linux, the hosts
file is stored as /etc/hosts
, and on Windows it is stored as C:\Windows\System32\Drivers\etc\hosts
.
If we want to share access to our virtual machine or demo something running on it, we can use Vagrant Share through Vagrant Cloud, which we will discuss in Chapter 9, HashiCorp Atlas.
It is also possible to have the IP address assigned via DHCP (typically, this will mean that your network's router will assign it an IP address):
config.vm.network "private_network", type: "dhcp"