Last Updated: November 06, 2018
·
1.742K
· mtchavez

Vagrant with VMware Viewing App From Mobile Device

Vagrant recently released support for VMware Fusion boxes. If you haven't switched over from VirtualBox I would at least recommend trying it out.

Should you need to test your app on a mobile device or let someone access the app running on your machine it is fairly simple. VMware Fusion has a configuration file that allows you to do port forwarding. If you are on a Mac it is located

/Library/Preferences/VMware\ Fusion/vmnet8/nat.conf.

There is a section marked by [incomingtcp] where you can define the port forwarding you want.

You can also configure Vagrant to do port forwarding if you want to add or override any port forwarding rules. See here for more information. Configure Vagrant to forward a port

config.vm.network :forwarded_port, guest: 80, host: 4000

Which will forward your port 4000 to your VMs port 80. After provisioning your Vagrant box thenat.conf file now should have your port forwarding rule. In the [incomingtcp] section there should be something similar to this

# VAGRANT-BEGIN: 
2222 = 172.16.167.128:22
4000 = 172.16.167.128:80
# VAGRANT-END:

This block means there are 2 port forwarding rules set up by Vagrant. It reads as

<external port number> = <VM’s IP address>:<VM’s port number>

So on your local machine you can visit localhost:4000 to access port 80 on your VM. The port 2222 forwards to the ssh port on your VM.

With this we can then expose our running app to a mobile device to test it out. On a Mac you can go to SystemPreferences > Sharing and make sure you have a computer name given so you can access your computer via my-mac.local. Once you have one make sure you can visit it.

Now we can add our port 4000 to our local address to visit the app. With your app already running in your VM visit my-mac.local:4000 from a mobile device or another computer on your local network. That should be it and you should see your app running. If you don't see anything make sure

1 The app is running in your VM on port 80
2 You have configured Vagrant to forward your VMs port 80
3 You use any default port forwarding set by Vagrant or VMWare in the nat.conf file if you are not setting your own.