3

I'm just trying to run my server locally. I'm on Windows and using Ruby on Rails on Windows is a pain, so I am using Vagrant. I am doing all of these commands from my Vagrant shell.

I've tried rails s and rails s -b 0.0.0.0. Both give me OK responses in the terminal:

=> Rails 5.2.3 application starting in development
=> Run `rails server -h` for more startup options
Puma starting in single mode...
* Version 3.12.1 (ruby 2.6.1-p33), codename: Llamas in Pajamas
* Min threads: 5, max threads: 5
* Environment: development

However, when I go to localhost:3000 in my browser, it gives me:

This site can't be reached.
localhost refused to connect.

When I tried to curl http://localhost:3000 get:

curl: (7) Failed to connect to localhost port 3000: Connection refused

I also have the following line of code in my Vagrantfile:

config.vm.network "forwarded_port", guest: 3000, host: 3000

Really don't know what to do next. Right now, I am installing Ubuntu ISO file (will be done in 5 hours, so that's quite a bit of time) to create a VirtualBox instance as backup if this doesn't work. Hoping I can find a fix for this.

Jessica
  • 1,083
  • 2
  • 12
  • 27
  • Is that all the output you get from `rails s -b 0.0.0.0` ? Here's mine: Puma starting in cluster mode... Version 3.12.0 (ruby 2.5.5-p157), codename: Llamas in Pajamas Min threads: 1, max threads: 1 Environment: development Process workers: 1 Preloading application Listening on tcp://0.0.0.0:3000 Use Ctrl-C to stop Worker 0 (pid: 6037) booted, phase: 0 One key thing to look for is `Listening on tcp://0.0.0.0:3000`. Maybe something is wrong in your config/puma.rb? Can you post if you have it? – Scott Jacobsen Jun 08 '19 at 02:26
  • One simple way to test if the port is open and take rails and puma out of the equation - in the vagrant terminal do `nc -l 3000` - netcat will listen on port 3000 and output anything it receives. Then in your browse connect to localhost:3000. nc should output `GET / HTTP/1.1` along with the other http headers sent by the browser. If there is no output something likely wrong with the port forwarding. If there is output something is likely wrong with the rails configuration. – Scott Jacobsen Jun 08 '19 at 02:49

3 Answers3

2

I would like to suggest you docker. Docker is not new, it was released in 2012 and since then has become one of the fastest-growing technologies in web development and devops.

Some of the advantages you will have if you start using it:

  • Reproducibility: A docker container is guaranteed to be identical on any system that can run docker and having a simple file you (and your team members) can run the system with the same specs really fast on another environment.
  • Isolation: Dependencies or settings within a container will not affect any installations or configurations on your computer.
  • Hub: You have thousands of well maintained images available including ruby and you can use them for faster experiment and get in the stuff that mater.
  • Docker is not vagrant, is a lot more and much more powerful.
  • Easy image upgrades: because images are versioned, it's a matter of to change a single tag.

Happy codding with the whale!

filipe
  • 1,957
  • 1
  • 10
  • 23
1

The key thing here is "localhost" on your Vagrant box and "localhost" on your machine are two different things. The port forwarding can often fix this, but if you have two Vagrant machines using the same port you may be sending traffic to the wrong one.

It's often better to get the Vagrant machine's IP and connect to that directly. If that IP keeps changing, you can lock it down:

config.vm.network "private_network", ip: "172.30.1.5"

Then you connect to http://172.130.1.5:3000/ predictably.

tadman
  • 208,517
  • 23
  • 234
  • 262
  • That is interesting. But, this post https://stackoverflow.com/questions/14870900/how-to-find-the-vagrant-ip says that the Vagrant box does not have an IP...? I thought my IP would be shared with Vagrant since this Vagrant is on my machine. – Jessica Jun 07 '19 at 17:52
  • By default it doesn't have an external IP address, as in one that can be accessed outside of your own machine. It always has some kind of IP address, though. You can't share the IP except through "NAT" networking mode, which is a default as far as I know. – tadman Jun 07 '19 at 18:20
  • 1
    So I added that line of code into my Vagrantfile. Then I ran ```rails s``` and went to ```http://172.130.1.5:3000/``` but it is still connection refusing. My command line says ```Listening on tcp://localhost:3000``` even though i specified the Vagrant machine's IP. – Jessica Jun 07 '19 at 18:33
  • If it's listening "localhost" then it won't accept external connections. You need to bind to `0.0.0.0` like you did in your question with `-b`. Binding to `127.0.0.1` (or `::1` in IPv6) means *only* local connections are permitted, and your Windows machine is not considered local, it's a different machine IP-wise. – tadman Jun 07 '19 at 18:38
  • Wait, isn't what you just said contradictory? Binding only allows local hosts, and my Windows machine isn't considered local. So... isn't that going to prevent me from connecting to the server? Anyway, I tried that, running ```rails s -b 0.0.0.0``` and going to ```http://172.130.1.5:3000/``` but it still failed. It doesn't make sense why the command line keeps saying: ```Listening on tcp://localhost:3000``` – Jessica Jun 07 '19 at 18:42
  • Oh, you must mean that by binding the private network to that IP address, my machine and the Vagrant machine are considered local. – Jessica Jun 07 '19 at 18:43
  • There's a difference between "local" as in "same physical machine" and "local" as in "localhost" to the IP stack. – tadman Jun 07 '19 at 19:09
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/194620/discussion-between-mcfloofenbork-and-tadman). – Jessica Jun 07 '19 at 19:10
  • If you have port forwarding enabled there is no need to connect to the ip address of the VM. You would also see an error starting the VM if another VM was already using that port. – Scott Jacobsen Jun 08 '19 at 02:26
  • That wasn't the issue. But I ended up getting my Rails server running by using VB. – Jessica Jun 10 '19 at 20:11
0

Resolved by running Ruby on Rails on UBUNTU VirtualBox machine.

Jessica
  • 1,083
  • 2
  • 12
  • 27