How to access Guest's port 3000 from Host?

963 views Asked by At

Here's my Vagrantfile:

# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure(2) do |config|
config.vm.box = "ubuntu-14.04-x64"

# Sync'd folders
config.vm.synced_folder ".",           "/vagrant",  disabled: true
config.vm.synced_folder "~/work",      "/home/vagrant/work", create: true
config.vm.synced_folder "~/apt-archives", "/var/cache/apt/archives/", create: true

# Ubuntu VM
config.vm.define "ubuntu" do |ubuntu|
ubuntu.vm.provision "shell", path: "provision.sh", privileged: false
ubuntu.vm.network "forwarded_port", guest: 3000, host: 8080   # http
ubuntu.vm.network "private_network", ip: "10.20.30.100"
ubuntu.vm.hostname = "ubuntu"

# VirtualBox Specific Stuff
# https://www.virtualbox.org/manual/ch08.html
config.vm.provider "virtualbox" do |vb|

  # Set more RAM
  vb.customize ["modifyvm", :id, "--memory", "2048"]

  # More CPU Cores
  vb.customize ["modifyvm", :id, "--cpus", "2"]

end # End config.vm.provider virtualbox
end # End config.vm.define ubuntu
end

For example, when I run rails app using port 3000, from the guest machine I would accessing http://localhost:3000.

But I'm trying to access the app via host's browser.

None of below worked:

http://10.20.30.100:8080

https://10.20.30.100:8080

http://10.20.30.100:3000

https://10.20.30.100:3000

Browser on the host's showing: ERR_CONNECTION_REFUSED

1

There are 1 answers

0
Myst On BEST ANSWER

For security reasons, Rails 4.2 limits remote access while in development mode. This is done by binding the server to 'localhost' rather than '0.0.0.0' ....

To access Rails working on a VM (such as one created by Vagrant), you need to change the default Rails IP binding back to '0.0.0.0'.

See the answers on the following StackOverflow Question, there are a number of different approaches suggested.

The idea is to get Rails running either by forcing the following command:

rails s -b 0.0.0.0

Or by hardcoding the binding into the Rails app (which I found less desirable):

# add this to config/boot.rb
require 'rails/commands/server'
module Rails
  class Server
    def default_options
      super.merge(Host:  '0.0.0.0')
    end
  end
end

Personally, I would have probably gone with the suggestion to use foreman and a Procfile:

# Procfile in Rails application root
web:     bundle exec rails s -b 0.0.0.0

This would allow, I believe, for better easier deployment synchronicity.