How to deploy a Ruby Sinatra Web Server Docker Image on Heroku - ruby

So, I'm trying to build a simple web server for personal practice with Ruby without Rails, dockerize it and deploy it to Heroku. But problems always arouse when the docker image has been deployed on Heroku. I think it's because When deploying a container app on Heroku, they require that Dockerfiles are configured to intake the randomly assigned port on a dyno - exposed as the $PORT environmental variable. Even though Docker allows for the usage of EXPOSE to specify which port for an image to use, Heroku ignores those commands and goes by the port assigned to a dyno.
So How do I exactly do this?
Error Message shown on Heroku when I clicked open app after successfully deployed the docker image:
Application error
An error occurred in the application and your page could not be served. If you are the application owner, check your logs for details. You can do this from the Heroku CLI with the command
My Web server has two functions: respond with your current IP and current time (in HTML).
I will be using Sinatra. I have successfully created the .rb file :
require 'sinatra'
require 'socket'
#find final_ip
addr_infos = Socket.ip_address_list
final_ip = ''
addr_infos.each do |addr_info|
final_ip = final_ip + ' ' + addr_info.ip_address
end
#find current time
cur_time = Time.now.ctime
#print to html
get '/' do
'<h2> Current Time: </h2> <br>' + cur_time + '<br><br>' + '<h2> Current IP Address: </h2> <br>'+ final_ip
end
config.ru file:
# config.ru
require './test'
run Sinatra::Application
Gemfile:
# Gemfile
source 'https://rubygems.org'
gem 'sinatra'
gem 'thin'
gem 'puma'
gem 'reel'
gem 'http'
gem 'webrick'
It run perfectly fine on localhost:4567 when I executed the command bundle install and ruby test.rb on terminal.
The following is the Dockerfile:
# Dockerfile
FROM ruby:3.0.1
WORKDIR /code
COPY . /code
RUN bundle install
EXPOSE 4567
EXPOSE $PORT
CMD ["bundle", "exec", "rackup", "--host", "0.0.0.0", "-p", "4567"]
There were no errors when building the docker image and deploying it on Heroku.
I am not sure how to solve this problem as I'm new to this Docker and Heroku world. Can somebody help me please? Thank you very much!

Related

How can I get Heroku's database information in a ruby script?

I created a rails app that is hosted on Heroku. Now I want to upload a ruby script that will populate my database.
This script already exists and is using
PGconn.connect( :hostaddr=>"127.0.0.1", :port=>5432, :dbname=>"myDB", :user=>"MyUser", :password=>'MyPass');
This will obviously not work on Heroku.
I tried to get it using:
Rails.configuration.database_configuration
But I get
uninitialized constant Rails
How can I get the information so I can connect to my Heroku DB? Is there any way to make the same code wotk both on my localhost and on Heroku?
(ps: I am using foreman and the .ENV file, if it helps)
Edit:
The solution I took:
require 'yaml'
require 'erb'
config = YAML.load(ERB.new(File.read(File.join("config","database.yml"))).result)
In config I have all the information I need to perform the DB access.
Why would you not just access ENV['DATABASE_URL'] and break it up into the constituent parts? To see how this is done, if you do heroku run bash and do cat config\database.yml you will see how Heroku does this itself.
To ensure that locally it works as well if you execute via foreman run <Scriptname> then it will be executed and the contents of your .env will be loaded.

Jekyll not serving site locally

Configuration: OSX 10.9.1, ruby 2.1.1 via RVM
I have created a new Jekyll site via the command jekyll new sitename.
I then enter that directory and issue the command jekyll serve.
I get the following notice:
Configuration file: /Users/George/sitename/_config.yml
Source: /Users/George/sitename
Destination: /Users/George/sitename/_site
Generating... done.
Server address: http://0.0.0.0:4000
Server running... press ctrl-c to stop.
However when attempting to visit http://localhost:4000/ or http://0.0.0.0:4000/ my browser endlessly attempts to load the page.
I have checked and the site was built correctly including an index.html in /Users/George/sitename/_site/.
The _config.yml looks like so:
name: sitename
markdown: maruku
pygments: true
Does anyone know why Jekyll could be failing to serve the site but not throwing any errors?
Edit: If I change the port to 6000 and attempt to serve it then my browser instantly gives a page not found so there must be something interfering with port 4000 however the issue still stands.
It's likely that something else is using port 4000 on your computer. You can find out by running the following command:
sudo lsof -i :4000
As you can run the server on another port, we'll move on to the next issue, setting the baseurl. In your _config.yml you can specify which baseurl to use, this will be the url from which all pages are served, so if you had baseurl: http://myawesomesite.com, Jekyll will expect them to be accessed from that url.
As you're working locally, you have two options:
Set the baseurl to / in your _config.yml:
baseurl: /
Launch Jekyll with the --baseurl flag:
$ jekyll serve -w --baseurl '/'
Additionally, you could specify the port as a flag:
$ jekyll server -w --baseurl '/' --port 4000
If you want further information about what's going on when you run jekyll, you can run the command with the --trace flag:
$ jekyll server -w --trace

Rails app only pointing to default rails index page

So I am new to deploying to an Ubuntu Server (12.04). I have nginx and unicorn installed. Everything seems to be working except for the fact that I don't know how to point it to my app. Currently I am pointing to 'home/administrator/apps/my_site/current/public' however all my files are in 'home/administrator/apps/my_site/app/views/'. I am currently only pointing to the static files like the error htmls and the default rails index.html. How do I get nginx to point to my views/app?
Thanks.
If you're using Nginx + Unicorn then you don't point Nginx at any path, you run up unicorn on the server and then have Nginx proxy all it's requests to the unicorn instance. I would recommend looking at using foreman and then it's foreman export upstart which exports ubuntu upstart scripts for controlling unicorn.
This link gives you details on how to config your nginx site.

"rackup config.ru" returns nothing?

I have my beginner Ruby application working perfectly on my local server using Rack and Sinatra. I'm in the process of getting it to work on my VPS webhost.
Here are my questions:
I tried running:
rackup config.ru
via SSH, and nothing happens, it just shows an empty line and I have to hit CNTRL+C to abort. Does anyone have ideas why?
If I run ruby main.rb, it successfully says Sinatra is taking the stage, but that's it, so I know you can't have it working with JUST that, but at least Sinatra is working properly.
I have a public_html folder. Is the main.rb and config.ru supposed to go in root "/" or is it supposed to go in public_html?
If it's the later, and I put a blank index.html inside public_html, will rackup know to 'replace' it with main.rb in / when it's running, and if it's not it just displays the index.html in public_html
When all is up and running, do I need to access the Ruby app via site.com:xxxx (xxxx=whatever port), or will I simply be able to go to site.com
You dont really need my code for main.rb, it just outputs "hello"
Here's config.ru:
require File.dirname(__FILE__) + "/main"
run Sinatra::Application
Q1, just rackup without config.ru
Q2, in general, the static folder name is public, not public_html unless you set it manually. other files, such as the main.rb, that is supposed to put into root directory, stay with the config.ru in the same folder if the code of main.rb as your given.
Q3, access by http://0.0.0.0:9292/ if you use the rackup by default setting to rack up the web server

Heroku RACK_ENV says "development" on Thin, but "staging" on Unicorn

I came across this behavior and was wondering if anyone else had seen it. I have a workaround so it's not a show-stopper.
I created a new app on Heroku with a Cedar stack. When demonstrating multiple environments I added the following config var:
heroku config:add RACK_ENV=staging --app appname
I visually verified that the environment var was set, then put the following route in my simple Sinatra example:
get '/?' do
ENV['RACK_ENV']
end
When I tested locally on my laptop, I received the expected development.
When I pushed to Heroku and hit the same route on herokuapp.com I got development instead of staging.
I switched the web server from Thin to Unicorn through the Procfile and pushed the changes back up to Heroku.
Now when I hit the route, I get the expected staging.
Has anyone else seen this? My workaround on another project where I was running Thin was to key the environment off of the New Relic app name. (I didn't switch to Unicorn because I need New Relic to work and currently Cedar and New Relic and Unicorn work together).
I had the same problem with sinatra and thin on the cedar stack using heroku's example sinatra app. The RACK_ENV refuses to be set to anything but development. (Heroku seems to think that it's RACK_ENV is set, since running 'heroku config' displays the environment you set, however in the app it's always development).
The same app on the bamboo stack had no problems.
EDIT: I submitted a ticket to heroku about this and got a really quick response which fixed the bug for me:
QUOTE:
It looks like there's a small bug in our default Procfile if you're using thin. Can you create a Procfile with the following in it?
web: bundle exec thin start -R config.ru -e $RACK_ENV -p $PORT
You can also set both your RACK_ENV and RAILS_ENV to staging using the Heroku gem ... then it works as expected. I think it may be a problem with Heroku.

Resources