Can't access sinatra service outside of container - ruby

I am getting ERR_EMPTY_RESPONSE in the browser after lunching the Sinatra container in docker. If I connect to the docker container and run curl localhost:4567 it works, but outside of the container - doesn't
web.rb
require 'sinatra'
set :bind, '0.0.0.0'
get '/' do
'Hello world!'
end
Dockerfile
FROM ruby:3.0.2
EXPOSE 4567
COPY ./web.rb .
RUN gem install sinatra
RUN gem install puma
CMD ["ruby", "web.rb"]

The solution was in binding application to 0.0.0.0 host
require 'sinatra'
set :bind, '0.0.0.0'
get '/' do
'Hello world!'
end

Related

How to make Sinatra listen on two ports with socat

I had a classical Sinatra application which was accessible on two ports. After migrating it to modular style the second port is not working anymore.
My initial implementation was:
require 'sinatra'
set :port, 8080
set :bind, '0.0.0.0'
----some routes-----
...
The resulting implementation was:
require 'sinatra/base'
require_rel 'lib'
class MyApp < Sinatra::Base
register Sinatra::SomeRegister
helpers Sinatra::SomeHelper
set :port, 8080
set :bind, '0.0.0.0'
----some routes-----
...
run!
end
The application is run using:
socat tcp-l:8181,fork,reuseaddr tcp:localhost:8080 &
ruby /path/my_app.rb
The application doesn't respond on port 8181 anymore.
The fix was to install socat first:
apt-get update && apt-get --allow-unauthenticated -y install socat

Expose port on all interfaces using Webrick

I am new to Ruby.
I am working with a script that deploys a HTTPServer using WEBrick.
When the script runs, it logs out :
check_1 | [2018-12-20 17:51:47] INFO WEBrick::HTTPServer#start: pid=1 port=4567
When I do a netstat on the machine, I notice that the service is listening only on 127.0.0.1
$ netstat -tulpn
root#c4a20aa7bd8c:/opt/service# netstat -tulpn
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:4567 0.0.0.0:* LISTEN 1/ruby1.9.3
I want to listen on all the IPs so that I can communicate with this service from another container.
How or what configs will be necessary ?
My script definition for the server is :
#!/usr/bin/ruby
require 'rubygems'
require 'sinatra'
require 'sinatra/reloader' if development?
also_reload 'config.yml'
require "sinatra/config_file"
require 'mechanize'
require "json"
require 'open-uri'
require 'mongo'
require 'uri'
require 'addressable/uri'
require 'cgi'
include Mongo
require 'nokogiri'
set :server, 'webrick'
The answer you're looking for is in the Sinatra config docs: http://sinatrarb.com/configuration.html
Specifically the :bind option. Example: set :bind, '0.0.0.0'

Heroku Postgres connection capped at 5/20

I have a Sinatra app running on Heroku free plan with Postgres, and I tried to set the database connection to 20 but after I pushed and run heroku pg:info the connection is still 5/20. It also wouldn't use all 20 connections even if I try to to a loader.io load test on it.
So I wonder what could I do to make it utilize all the 20 available connections or I am having some misunderstanding here?
# database.yml (for ActiveRecord)
production:
adapter: postgresql
encoding: unicode
database: mydb
username: <%= ENV['PG_USER'] %>
password: <%= ENV['PG_PASS'] %>
pool: <%= ENV['DB_POOL'] || ENV['RAILS_MAX_THREADS'] || 20 %>
# Procfile
web: bundle exec puma -t 5:20 -p $PORT
# puma.rb
preload_app!
on_worker_boot do
ActiveSupport.on_load(:active_record) do
ActiveRecord::Base.establish_connection
end
end
# config.ru
require_relative './config/init'
use Rack::SSL if ENV['RACK_ENV'] == 'production'
run Sinatra::Application
The DB_POOL variable has been set to 20 on Heroku but it still doesn't do anything. Any help will be appreciated.
I fixed this by running puma with -C config/puma.rb

How to run Sinatra App in Passenger with Nginx?

I'm trying to run a very simple Sinatra app using my existing Nginx & Passenger setup. I'm familiar with running Rails on Passenger but this is my first time setting up Sinatra.
I've installed Sinatra through bundler and RVM. Take a look at my configuration and tell me what I'm doing wrong.
Nginx conf:
server {
listen 80;
server_name demo.my-example.com;
root /home/user/demo.my-example.com/sinatra;
passenger_ruby /usr/local/rvm/wrappers/ruby-2.3.1#my_gemset/ruby;
passenger_enabled on;
}
/home/user/demo.my-example.com/sinatra/config.ru
require 'rubygems'
Gem.clear_paths
disable :run, :reload
set :environment, :production
require "./stripe"
run StripeApp
/home/user/demo.my-example.com/sinatra/stripe.rb
require 'sinatra/base'
class StripeApp < Sinatra::Base
get '/' do
"Hello world"
end
end
/home/user/demo.my-example.com/sinatra/config.ru
require 'rubygems'
Gem.clear_paths
require "./stripe"
run StripeApp

Sinatra on Nginx configuration - what's wrong?

I followed this tutorial more or less... I installed the passenger gem, executed passenger-install-ginx-module, sucessfully installed nginx and inserted this into the config:
server {
listen 80;
server_name localhost;
root /home/admin/sintest/public; # <--- be sure to point to 'public'!
passenger_enabled on;
}
In /home/admin/sintest I have: an empty public folder,
the config.ru:
require 'sinatra'
set :env, :production
disable :run
require './app.rb' #the app itself
run Sinatra::Application
and a test sinatra app.rb:
require 'sinatra'
get '/' do
"hello world!"
end
Now when I run nginx and open up http://localhost what I get is: 403 Forbidden
What am I doing wrong? Have I missed something?
Make sure that the user nginx is running as (in most cases 'nobody' or 'www-data') has permission to read the contents of your home directory /home/admin.
Also you can look into the nginx logs and read exactly what the error was.
I had the same error until I added passenger_root and passenger_ruby directives in the http block.

Resources