3
votes

I'm trying to setup a simple Rails app in Vagrant. This is my first time setting up Vagrant+rails. Here's the Vagrant file. As you can see I'm mapping localhost:3000 to guest:3000.

Vagrant.configure(2) do |config|
    config.vm.box = "ubuntu/trusty64"
    config.vm.network 'forwarded_port', guest: 3000, host: 3000
    config.vm.provision 'shell', path: 'bootstrap/bootstrap_vagrant.sh'
end

Here is my bootstrap file, which I've stripped down for brevity:

#!/usr/bin/env bash

set -e
apt-get update
echo "LC_ALL=\"en_US.UTF-8\"" >> /etc/default/locale
locale-gen en_US.UTF-8
update-locale LANG=en_US.UTF-8
apt-get -y install build-essential curl git-core python-software-properties htop vim
apt-get -y install nodejs # needed by Rails to have a Javascript runtime
apt-get -y install zlib1g-dev libssl-dev libreadline6-dev libyaml-dev libncurses5-dev libxml2-dev libxslt-dev
sudo -u vagrant printf 'cd /vagrant\n' >> /home/vagrant/.profile
su vagrant -c "mkdir -p /home/vagrant/downloads; cd /home/vagrant/downloads; \
                wget --no-check-certificate https://ftp.ruby-lang.org/pub/ruby/2.1/ruby-2.1.5.tar.gz; \
                tar -xvf ruby-2.1.5.tar.gz; cd ruby-2.1.5; \
                mkdir -p /home/vagrant/ruby; \
                ./configure --prefix=/home/vagrant/ruby --disable-install-doc; \
                make; make install;"
sudo -u vagrant printf 'export PATH=/home/vagrant/ruby/bin:$PATH\n' >> /home/vagrant/.profile
su vagrant -c "/home/vagrant/ruby/bin/gem install bundler --no-ri --no-rdoc"
su vagrant -c "/home/vagrant/ruby/bin/gem install rails"
apt-get -y install libsqlite3-dev
su vagrant -c "export PATH=/home/vagrant/ruby/bin:$PATH; cd /vagrant; bundle;"
su vagrant -c "export PATH=/home/vagrant/ruby/bin:$PATH; cd /vagrant; bundle exec rake db:create;"
su vagrant -c "export PATH=/home/vagrant/ruby/bin:$PATH; cd /vagrant; bundle exec rake db:setup;"
su vagrant -c "export PATH=/home/vagrant/ruby/bin:$PATH; cd /vagrant; bundle exec rake db:seed;"

During reload of the vagrant environment it appears that the port forwarding is working:

==> default: Forwarding ports... default: 3000 => 3000 (adapter 1) default: 22 => 2222 (adapter 1)

After setting this up, I ssh to the vagrant box, and created a rails app, then started it by running bundle exec rails s.

If I ssh to the vagrant environment, and run curl http://localhost:3000, I see the Rails welcome page. So the rails server is running fine. But if I go to http://localhost:3000 from my laptop (host environment), it doesn't load. Hitting that via curl doesn't help either.

$ curl http://localhost:3000
curl: (52) Empty reply from server

My host environment is OSX 10.9.5. (Mavericks). Firewall is disabled in preferences.

1

1 Answers

7
votes

Your development server is not listening on the right IP address, this is why you can access it from inside Vagrant guest (localhost, 127.0.0.1 - default binding for rails server), but not from outside (your laptop - host)

Tell your development server to listen on your publicly accessible interface.

In order to make the server listen on ALL interfaces, bind it to IP 0.0.0.0:

bin/rails server -b 0.0.0.0