5.3. Port forwarding

Private networking works best if you only need to access the vm from the host. However, there are situations when you need other machines in your network to gain access to the vm. Before you go to public networking, you might want to consider port forwarding because it is possible to let other machines gain access to the vm without totally exposing the guest machine. Think of the port forwarding option as a combination of public and private networking. We get to select which application services (ports) we will expose to the public. The ports are not all exposed as a default.

Example 5.3. Port forwarding configuration

Vagrant.configure(2) do |config|
  config.vm.network "forwarded_port", guest:80, host:8080 1 2
end

1

guest:80 means that an application on the vm, the guest OS, is bound to port 80

2

host:8080 means that if a client like a web browser on your actual machine, the host, goes to http://localhost:8080, that request will be forwarded to the application on the vm that is listening on port 80