Docker error localhost didn’t send any data - ruby-on-rails

My development team and I have been having the hardest time getting our Ruby on Rails application to dockerize. All tutorials and answers out there seem to be years old and thus outdated.
After a few days of researching things and trying multiple techniques, we are finally finding answers. So, this post is to ask the most recent question and provide the answer we got.
In a new Ruby on Rails application running Ruby 2.7.0 and Rails 6.0.3.4, we would be able to run a rails server and get the standard Ruby screen at localhost:3000. But, after dockerizing the app and going to localhost:8000 as that is supposedly the default port for a docker image, we would get an error stating that the localhost didn't send any data.
This is the question, what is causing this to occur? We tried localhost and 127.0.0.1 and even the local machine's name that we got by running hostname in the terminal. None of these worked listening on port 3000 or port 8000.

The answer that we came up with for this problem is that the app was still saying that the information requests should be coming from port 3000 even though docker was looking at port 8000. So, we went into config/puma.rb and changed the code like this:
-port ENV.fetch("PORT") { 3000 }
+port ENV.fetch("PORT") { 8000 }
Basically, if you are having this same issue, look in the puma.rb file for the line that designates port 3000 and change it to port 8000. Once that happened, we were in business.
NOTE: The - and + signs in the code above are from the git repository showing that the first line was deleted and the second line added. Those symbols should not be in the actual code.
Good luck and happy codeing!

Related

`rails server` using Puma and domain name pointing to 127.0.0.1

I have a Rails application that uses subdomains (legacy application, I've been wanting to change that, not yet). I deployed my app to Heroku and I've started to test Puma because it's the recommended choice for Heroku and the default in the upcoming release of Rails. When I used WEBrick (locally) I was able to test my subdomains using a DNS record that pointed to 127.0.0.1 such as vcap.me, specifically http://vcap.me:3000/ would point to my app and http://abcde.vcap.me:3000/ will correctly set the subdomian to "abcde".
Simply adding gem 'puma' to my Gemfile and runnning bundle, causes rails server to start Puma. Except none of the test domains work: http://localhost:3000/ works, but not http://vcap.me:3000/ or http://lvh.me:3000/
Chrome simply says:
"This webpage is not available
ERR_CONNECTION_REFUSED"
Firefox:
"Unable to connect
Firefox can't establish a connection to the server at vcap.me:3000.
..."
I haven't found a cause/solution, but I suspect it has to do with non HTTP TCP requests supported by Puma, except right know, I'm simply trying a HTTP request through the browser.
Just for the curious, if you haven't heard about vcap.me and similar domains, it's simply a DNS record that points to localhost:
$ dig vcap.me
...
vcap.me. 3048 IN A 127.0.0.1
...
$ dig a.vcap.me
...
a.vcap.me. 3600 IN A 127.0.0.1
...
I feel ashamed, #maxd posted a solution to a very similar question: https://stackoverflow.com/a/28745407/637094 and it works. I still don't understand why I need to bind to vcap.me and I didn't before when I used WEBrick.
rails server -p 3000 -b vcap.me
I'll leave the question open, so maybe someone can expand and we all get a better picture of what's going on
This was issue #782 in the Puma server that was solved on July 18, 2016 here.
Your use of domains like vcap.me was not the issue. That domain be resolved to 127.0.0.1 by the DNS server. The issue was, before it was fixed, that on some systems Puma would by default bind only to the IPv6 resolution of localhost, i.e. ::1. Since vcap.me does not provide a IPv6 resolution, you could not reach Puma by calling http://vcap.me:3000/.
Your observation that rails server -p 3000 -b vcap.me solved the issue is because that is equivalent to rails server -p 3000 -b 127.0.0.1. After that, the server's address matched the IPv4-only name resolution of vcap.me.
Anyway, it's an issue of the past. Now by default, Puma binds to both the IPv4 and IPv6 resolutions of localhost.

How do I make my ruby on rails app respond to external requests (visible to the public on the internet)?

Problem:
My rails app (on my local machine) only responds to requests sent from the same machine to localhost, 127.0.0.1, or my internal ip address. When I try to hit it using my internet ip or from any other machine, inside or outside of my network, it just times out. I'm on Mac OS 10.9.1, ruby 1.9.3, rails 4.0.0.
I've done a lot of searching but all I can find is problems where people didn't forward their ports or bind the right ip.
Here are the areas I've investigated:
Ports -
I've tried several different ports. I configured my router to forward every port I tried but got the same result. I thought maybe there was a problem with the router so I built a simple server in Java and bound all the same ports I was binding with my rails app. Sure enough, when I hit the Java app using my internet ip it worked just fine so the router/firewall/port forwarding isn't the problem. Also, I run an apache server on port 80 and that has never had any problems. I turned apache off and tried port 80 for my rails app but that didn't fix the problem.
Rails Server -
I started with WEBrick and I thought that perhaps there was some setting inside that blocked external requests. I searched google extensively and found nothing on that matter. Just to be safe I installed Thin and got the exact same result I did with WEBrick. One interesting thing is that when the rails server is started, the external request takes a long time to time-out, but the server console displays no output at all. However if I try to send the same request w/out starting the server at all it fails immediately.
User Permissions -
I started the server with root (i'm starting to just shoot in the dark here) and it had no effect.
Environment -
I was starting in development environment originally because I'm developing but just for fun I tried starting in production and it also made no difference.
PLEASE HELP ME SMART PEOPLE
Update:
I installed the app on my Ubuntu machine and it doesn't have this problem! So that suggests the problem may have something to do with Mac OS.
SOLVED:
It turns out that in the System Preferences -> Security & Privacy -> Firewall in Mac OS, it was somehow set to block incoming connections to Ruby 1.9.3. I must have accidentally set that some time ago.
The problem is you are probably trying to request the page from your local machine (or any computer on your local network, behind your firewall) to your public IP expecting a result... not unless you setup routes through your firewall for this (and not usually available on a consumer level router... linksys, dlink, etc)
So forward port 80 if you are using something like pow, or 3000 for web bricks default port to your local machine
Then have someone outside your local lan request your external (public) IP
This may be related: Rails 3.1 on Ubuntu 11.10 under VirtualBox very slow
Your mention of slowness combined with the use of webrick makes me think you've got some reverse-DNS lookup awfulness going on. A quick first step is hacking /etc/hosts to bypass this lookup.
The situation I dealt with on Ubuntu was solved in the short-term by hacking /etc/hosts. You could do this quick hack in order to see if it is indeed just webrick's reverse-DNS lookup. Edit /etc/hosts and add a line for the external user's IP address, something like this:
156.123.48.55 TestPerson
Replace the IP address with the tester's IP address. Since you said you can get the external request to hit an Apache server on port 80, you can grab their IP address from the Apache access logs if necessary, otherwise just ask the person testing.
You could also try a different web server, such as unicorn, which may help out. Add "gem unicorn-rails" to your Gemfile, run bundle install, and then (according to their docs), rails server will just use unicorn directly.
With any local server, you'll need to correctly configure port forwarding on your firewall. Like said by CaptChrisD, tests must be done by an external IP/browser (if you own a server, ssh on it, then w3m to test).
I already had same symptom (server started => timeout, server stopped => fail) and the origin was an issue with firewall configuration. I think it is your problem.
With MacOS, Pow is really awesome: installation is easy, no configuration required (no /etc/hosts…). Moreover, they give you a hook for external access to your virtualhosts (but you still need port forwarding on your firewall).
Otherwise, there is other solutions like Forward to do it without firewall configuration (30-days free trial).
Hope this helps!

Rails server is running, but cannot connect to localhost:3000

I am learning Ruby on Rails with railstutorial.org
I had set everything up and working fine from Chapter 1. However, all of a sudden my next app has an issue.
I run "rails server"
=> Booting WEBrick
=> Rails 3.2.9 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
[2012-11-15 00:45:08] INFO WEBrick 1.3.1
[2012-11-15 00:45:08] INFO ruby 1.9.3 (2012-11-10) [x86_64-linux]
[2012-11-15 00:45:08] INFO WEBrick::HTTPServer#start: pid=2752 port=3000
Seems to be working fine, just like with my previous app.
However, I try connecting to localhost:3000 , 0.0.0.0:3000 , 127.0.0.1:3000 on various browsers and they all cannot establish a connection to the server.
Some things to note:
-I was able to connect to localhost just a while ago--it just seems like it suddenly stopped working out of the blue.
-My first app was working perfectly fine, but now it doesn't work for my first app either.
-I don't have firewalls blocking the port, and my hosts file is not the problem.
-I am on Ubuntu 12.10
I almost always find solutions via search, but not this time.. so I need some help please. It's very frustrating as I feel like it's a simple problem that I spent way too long being stuck on.
Thank you.
Try running it in some other port like say 3001 as:
rails server -p 3001
If its working than than try it again on 3000 as the command above.
I thing some other software is using your 3000 port that's why its not responding.
Or for some advanced things see here
with rails 4.2.0, the server binds to localhost by default, instead of 0.0.0.0. When working with a rails in a virtual box, accessing the server from the host computer, the binding address needs to be 0.0.0.0
Start rails server with -b0.0.0.0 to make the rails server accessible from the host computer/browser.
http://guides.rubyonrails.org/4_2_release_notes.html#default-host-for-rails-server
https://github.com/samuelkadolph/unicorn-rails/issues/12#issuecomment-60875268
Make sure you run rake db:create before launching rails s.
I'm using rails 5.0.0.beta3 and was running into this issue. #andrewleung's answer helped me a lot.
It seems like Rails default binding address is messed up on my computer (macOS 10.11.6) ; on some others, it works fine.
The simple solution is just to use rails server -b 127.0.0.1. You can then access your localhost:3000.
My guess here is (hinted from https://serverfault.com/a/544188) that localhost binding is messed up on my computer whereas 127.0.0.1 is more specific.
I had the same issues and i realized it was in the config/environment/production.rb file where config.assets.compile = false must be changed to config.assets.compile = true
However this might in a way render some javascript and sass elements unworking
The issue that it turned out I was having was that my VM had run out of hard drive space and there wasn't even enough left to create the server.pid file. For some reason though, it wasn't throwing an error for this, as the file was being created, but was left blank.
I run into the same issue. It turned out that browser-sync is also running on localhost:3000.
Due to some Rails developer would use browser-sync to test out the front end scripts quickly, I think that could be a popular reason that port 3000 is used.
check your /etc/hosts file..is ip 0.0.0.0 or localhost pointing to some other address.
for me...I was behind a proxy at work and had to do rails s -b 0.0.0.0 -p 3000

pylons on production server fedora 8

I'm interested in learning some python, and thought Pylons would be a good starting point (after spending 2 days trying to get django working -- to no avail).
I have an Amazon EC2 instance with Fedora 8 on it. It is a bare-bones install. I am halfway through my second day of trying to get it to work. I have mod_wsgi installed. I have Apache (though that's a later task to tackle). I have easy_install, paster is working fine; basically all of the pre-requisites mentioned throughout the Pylons docs.
I can't for the life of me get the thing to work. And I can't seem to find a coherent walkthough anywhere that lists all the steps necessary. There is tons of info out there, but it is all scattered. Wsgi this, python that. Google, google, google... "47 million results found for 'socket.error:(lol, 'Yous a goofs')".
So, this is my latest attempt:
apachectl -k stop
cd /home/
paster create -t pylons test
[blah blah.. ok]
cd test
nano development.ini
[hmm, last time I changed the host from 127.0.0.1 to my domain name or url, it threw an error like socket.error: (99, 'Cannot assign requested address')... I'll just leave it]
[open port 5000 on firewall]
paster serve development.ini
[firefox->url:5000]
Firefox can't establish a connection to the server
Doing these steps locally works as expected.
This is just a test to see if I can get it to work at all, which I can't. If I get it to work, then is the task of getting it to work with apache.
My madness is that I'd like to play around a little developing and deploying before diving into a full-fledged project. So far: self, I am dissapoint.
[server:main]
use = egg:Paste#http
host = 0.0.0.0
port = 5000
If you leave it as 127.0.0.1, you'll only be able to surf the site from the ECS instance. By putting in the IP of your ECS instance, or 0.0.0.0 which will listen to all available IPs, it should work.
Unless you have something else listening on port 5000, it should work.
If you are using the hostname, and the hostname resolves to an IP address on your machine it should work. If you use a URL, paster won't be able to resolve the domain name properly. I'm guessing that when you used the domain name, it didn't properly resolve it which generated the error you received. 0.0.0.0 or the IP address in that case should fix it.
I'm not 100% sure whether you used a virtualenv for your pylons installation, but, it is generally a good idea since you don't need to alter the server's environment to install new libraries.
http://pylonshq.com/docs/en/1.0/gettingstarted/#installing

Cannot access http://localhost:3000

I am trying to learn Ruby on Rails, I have followed the instructions from this page to get rails installed on my PC.
I am also trying to follow this webcast to try and learn the language and framework. Everything is working so far, apart from the fact that I cannot access
http://localhost:3000
http://0.0.0.0:3000
http://127.0.0.1:3000, or
http://<actual IP address>:3000
locally. If I try the from another PC on my network then it works great. I have tried in Chrome, Firefox and IE7 but none work.
Has anyone else had this problem?
EDIT: Typical!! It's started working now. I have no idea why, I am typing the exact same address in to the address bar and it now works. But only if I use http://127.0.0.1:3000, localhost doesn't work. I do run IIS ASP.NET/ASP websites on this machine, and they work fine with localhost.
EDIT 2: If I trying pinging localhost it actually says
Reply from ::1: time<1ms
0.0.0.0 yields...
PING: transmit failed, error code 1214
only 127.0.0.1 seems to work. I did have IPv6 turned on, so I've disabled that and will try again tomorrow to see if a reboot helps.
I had this issue as well with Vagrant. The secret is to run
bin/rails server -b 0.0.0.0
What 0.0.0.0 means is to listen to all interfaces, not just on localhost. The alternative is to SSH into the vagrant machine with a SOCKS proxy.
ssh -C -D 8080 vagrant#localhost
Open up your Internet explorer proxy settings and set the SOCKS v5 proxy to port 8080.
In order to access http://localhost:3000 you need to run the local Rails server in a terminal window:
$ rails server
This is described in this section of the Rails Tutorial book.
Note: I am the presenter of the screencasts in question.
Can you telnet to the port? Try:
telnet localhost 3000
That should tell you if the ports unavailable at the TCP level, or whether something's going on at the HTTP level.
Ping might not work if ICMP is disabled, which could be completely unrelated to your issue.
Also, try looking in your hosts file for any redirections:
c:\Windows\System32\drivers\etc\hosts
One way is to do the simple
rails s
Another (more specifically) is
rails start localhost 0.0.0.0
or
rails start 0.0.0.0
another way is
rails s 0.0.0.0
all of these works
I had an epic issue with my System Path (Environmental Variable) not being set properly.
Simple copy paste job, unplugged the 1 week stalled Ruby installation.
Control Panel > System > Advanced tab > Environment Variables > System variables > Path... Edit...
Make sure it contains these paths (among others)...
%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem
I had your same identical problem. You have simply to run
$ rails server
from inside the folder of your application, not outside...so, if you have your application in
C:\Sites\myapp\blog
and blog is the folder where live your app, the command has to be prompted from here. Launching it from myapp folder won't work.
Are you possibly using a proxy but haven't listed localhost as proxy exception?
Have you tried disabling your firewall? They can cause pesky problems at times.

Resources