Easy communication with Rails VirtualBox server

In a recent post I talked about how to setup a Ruby on Rails environment for Mac OS or Ubuntu by itself and in a VirtualBox. In using my VirtualBox Ubuntu server on my Windows laptop, I’ve been annoyed by the constantly changing IP address when networking is configured to Bridge Adapter, so after a little Google sleuthing I was able to find an easy to implement solution: port forwarding.

The Problem

When dealing with an Ubuntu server in a Virtual Machine (VM), you will want to be able to easily access the running applications from your host machine browser and SSH into your server via Cygwin or PuTTY. When running the VM’s networking adapter through Bridge Adapter mode though, you have to deal with a constantly changing IP address thanks to a locked down DHCP only networking configuration.

The Solution

The easiest way to get around these issues is to run your VM networking adapter in standard NAT mode. Not only does this lock things down a little better, but it provides a stable IP address that you can make some persistent references to.

1. Set VirtualBox Network Device to NAT

This is the default setting when you create a new VM, so you might not even need to change this.

Screenshot+on+12.21.2012+at+11.23.39+PM

2. Get Your VM Server’s IP Address

You can use the built in Port Forwarding settings for your VM to point a few unused ports on your host machine to some expected ports on your VM server. You will need to know the IP of your VM before setting this up, but it will most likely be VirtualBox’s NAT default of 10.0.2.15. If you are running Ubuntu server, this is conveniently shown when you log in:

Screenshot+on+12.21.2012+at+11.32.13+PM

Notice the line IP address for eth0 – this is the IP address to use in your port forwarding settings.

3. Setup Port Forwarding

Now that you know your VM server’s IP address its time to setup port forwarding. For a Rails environment you may want to consider at least setting up two ports for easy access:

Host Port Guest Port
SSH 2280 22
Rails 3080 3000

These port forwards will give you the convenience of being able to access your running Rails application at the persistent location of:

You will also be able to easily SSH into your VM server using Cygwin or your SSH terminal application of choice:

Just click on the Port Forwarding button in your VM network device configuration area and setup your Host IP (127.0.0.1) to forward to your Guest IP (10.0.2.15 in our example) for each port combination in the table listed above and you’re good to go!

Screenshot+on+12.21.2012+at+11.40.10+PM

UPDATE:
I just found out an area where this solution falls over – you can’t use this exact setup when trying to serve to other machines on your network. Luckily, the solution is easy – just eliminate the Host IP and Guest IP from your port forwarding rules. This will all you to access a running server in your VirtualBox via the host computer’s IP instead of being limited to localhost (such as when you need to test things on another device). Be advised that this will expose your VirtualBox’s to anyone on your network, so make sure you set your firewall settings appropriately (Windows firewall will prompt you for approval the moment you make this change – I recommend you keep things like port 25 and 22 restricted to your host machine’s IP for security).

PRO TIP:
Replace your XAMPP host completely and setup a LAMP stack in your VirtualBox. Map port 80 directly (instead of through a proxy port like 8080) and your regular web traffic will route right into your VirtualBox. Now you can have a true LAMP stack running on any OS that runs VirtualBox!

1 thought on “Easy communication with Rails VirtualBox server”

Leave a Reply