Puma server inside heroku container fails to connect to port - ruby-on-rails

I have a docker container: (called Dockerfile.web, relevant parts)
ARG PORT=3000
ENV PORT=$PORT
EXPOSE $PORT
ENTRYPOINT ["docker-entrypoint.sh"]
CMD ["rails", "server"]
Puma: config/puma.rb
before_fork do
ActiveRecord::Base.connection_pool.disconnect! if defined? ActiveRecord
end
on_worker_boot do
ActiveRecord::Base.establish_connection if defined? ActiveRecord
end
preload_app!
So when I run heroku container:push web followed by heroku container:release web, the app deploys, but the puma server doesn't come up b/c the port cannot be connected to:
Booting Puma
Rails 5.2.2 application starting in development
Run `rails server -h` for more startup options
Puma starting in cluster mode...
* Version 3.12.0 (ruby 2.6.0-p0), codename: Llamas in Pajamas
* Min threads: 5, max threads: 5
* Environment: development
* Process workers: 1
* Preloading application
* Listening on tcp://localhost:41626
Use Ctrl-C to stop
- Worker 0 (pid: 28) booted, phase: 0
Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
Stopping process with SIGKILL
I'm following the official guide but I haven't added the sections:
rackup DefaultRackup
port ENV['PORT'] || 3000
environment ENV['RACK_ENV'] || 'development'
Where could my error be? Any ideas?

I'm not sure why you left this part out:
port ENV['PORT'] || 3000
You can't pick your own port. Use the $PORT that Heroku gives you.

For people arriving here, the solution was to invoke the puma server directly from the docker CMD so that all the settings in the config file are applied. The reason is that on heroku, stuff like port is added dynamically, so container images (which are built out earlier) will not have those images. Somehow using the $PORT values inside Dockerfile also did not help. The solution was to do
CMD ["bundle", "exec", "puma", "-C", "config/puma.rb"]
# or if you prefer the bash style
CMD bundle exec puma -C config/puma.rb

Related

Rails not connecting on port 3000

I'm not able to connect to localhost:3000 (when I try, it directed me to Google SERP - server log shows the following) but localhost:3001
=> Booting Puma
=> Rails 5.2.1 application starting in development
=> Run `rails server -h` for more startup options
Puma starting in single mode...
* Version 3.12.0 (ruby 2.5.1-p57), codename: Llamas in Pajamas
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://0.0.0.0:3000
Use Ctrl-C to stop
when sudo nano /etc/hosts, it showed 127.0.0.1 localhost
127.0.0.1:3000 works though
Where did I go wrong?
This happened for the first time for me.
When I tried
wget http://0.0.0.0:3000
--2018-09-24 01:46:21-- http://0.0.0.0:3000/
Connecting to 0.0.0.0:3000... failed: Connection refused.
Could you try start rails server with:
$ bundle exec rails server -b 0
Then go to http://localhost:3000

Multiple concurrent rails servers with binding

I have a rails staging server available to my LAN as follows:
rails server --binding=0.0.0.0 -p 3000
I would now like to open up a second, concurrent rails server to my LAN as follows:
rails server --binding=0.0.0.0 -p 3001
Unfortunatly, I am getting this error message:
...
=> Booting Puma
=> Rails 5.1.1 application starting in development on http://0.0.0.0:3001
=> Run `rails server -h` for more startup options
A server is already running. ...
The error only exists if I use --binding=0.0.0.0 on both servers, which is necessary if I want it to be accessible to my LAN.
How can I open up multiple rails servers on the same machine to the LAN, not just localhost?
EDIT:
After trying Vasfed's solution, e.g.
rails server --binding=0.0.0.0 -p 3000 --pid=tmp/pids/server0.pid
rails server --binding=0.0.0.0 -p 3001 --pid=tmp/pids/server1.pid
the problem persists, but this time I have more information. It seems related to a port 9292 being opened...
=> Booting Puma
=> Rails 5.1.1 application starting in development on http://0.0.0.0:3001
=> Run `rails server -h` for more startup options
*** SIGUSR2 not implemented, signal based restart unavailable!
*** SIGUSR1 not implemented, signal based restart unavailable!
*** SIGHUP not implemented, signal based logs reopening unavailable!
Puma starting in single mode...
* Version 3.9.1 (ruby 2.4.1-p111), codename: Private Caller
* Min threads: 0, max threads: 16
* Environment: development
* Listening on tcp://0.0.0.0:9292
Exiting
C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/puma-3.9.1/lib/puma/binder.rb:269:in `initialize': Only one usage of each socket address (protocol/network address/port) is normally permitted. - bind(2) for "0.0.0.0" port 9292 (Errno::EADDRINUSE)
Rails checks if a pid file is already present. To run two copies of single app, you should alter pids too:
rails server --binding=0.0.0.0 -p 3000 --pid=tmp/pids/server1.pid
rails server --binding=0.0.0.0 -p 3001 --pid=tmp/pids/server2.pid

Puma starting in single mode but not really start

I'm configuring Jenkins to deploy in production my rails application.
At the end of jenkings, i can see:
+ bin/bundle exec puma -C config/puma.rb
Puma starting in single mode...
* Version 3.4.0 (ruby 2.2.2-p95), codename: Owl Bowl Brawl
* Min threads: 0, max threads: 16
* Environment: production
* Daemonizing...
Finished: SUCCESS
After the script, puma is not running ...
config/puma.rb
threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 }.to_i
threads threads_count, threads_count
railsenv = 'production'
directory '/var/lib/jenkins/workspace/MyRailsApp'
environment railsenv
daemonize true
pidfile "/tmp/pids/puma-#{railsenv}.pid"
state_path "/tmp/pids/puma-#{railsenv}.state"
threads 0, 16
bind "unix:///tmp/my-rails-app.sock"
port ENV.fetch("PORT") { 3000 }
preload_app!
on_worker_boot do
ActiveRecord::Base.establish_connection if defined?(ActiveRecord)
end
plugin :tmp_restart
What's the best way to start/restart puma with Jenkins ?

Starting a rails app to the external ip address

I have a ruby on rails application and I am trying to run it on the external ip of my google compute engine ubuntu 14.04 LTS VM.
I try rails server -e production
and the output is:
=> Booting Puma
=> Rails 4.2.4 application starting in production on http://localhost:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
Puma 2.14.0 starting...
* Min threads: 0, max threads: 16
* Environment: production
* Listening on tcp://localhost:3000
I do not want it to be at that location; I want it to be viewable from the external ip address of the server.
Part of the issue is that I do not know if this is a rails, a puma, or a google compute engine question.
Note: I can't see if it actually launching at localhost:3000 because the VM is just a terminal.
(I am assuming you are using nginx, if not, apt-get install nginx; service nginx start)
If possible, show your nginx.conf (/etc/nginx/nginx.conf) and default.conf (/etc/nginx/sites-available/default.conf)
Since you are using puma (I use it too) you should setup nginx conf file and set server upstream equal to puma's binding.
# /etc/nginx/sites-available/default.conf
upstream your_app {
server 127.0.0.0.1:9292;
}
server {
listen 80;
server_name your_domain;
root /usr/share/nginx/your_app/public;
# or, if you are using capistrano
root /usr/share/nginx/your_app/current/public;
location / {
proxy_pass http://your_app; # equal to upstream "name"
...
}
....
}
# config/puma.rb
[...]
bind "tcp://127.0.0.1:9292"
And execute puma's server
$ bundle exec puma RAILS_ENV=production &
After doing this steps and if the application still doesn't work, output your /var/log/nginx/error.log, nginx.conf and default.conf

Production mode is not running on port 80 (Rails)

I don't understand why it's not running on port 80 instead of port 3000 when I run the command RAILS_ENV=production rails s on the same line. I want it to run in production mode but it's not running on the correct port. Anyone know why? I'm trying to use Rubber but I haven't ran any commands for it only just changed some of the files like it says in this tutorial.
root#ip-000-00-00-000:/home/ubuntu/Git/# RAILS_ENV=production rails s
=> Booting Thin
=> Rails 3.2.11 application starting in production on \http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
\>> Thin web server (v1.5.1 codename Straight Razor)
\>> Maximum connections set to 1024
\>> Listening on 0.0.0.0:3000, CTRL+C to stop
^C>> Stopping ...
Exiting
webrick runs on port 3000 by default(even in production mode). Pass the port number explicitly if you want to run on a different port.

Resources