Skip to content

Instantly share code, notes, and snippets.

@gabrieljoelc
Last active May 27, 2019 20:11
Show Gist options
  • Save gabrieljoelc/8c04941042e9241a41b840cccf1ad5fb to your computer and use it in GitHub Desktop.
Save gabrieljoelc/8c04941042e9241a41b840cccf1ad5fb to your computer and use it in GitHub Desktop.
Heroku Docker Rails 5.1.3, Ruby 2.4.2 (I need to add a Heroku template to https://github.com/nickjj/orats)

Steps to deploy Dockerized Rails app to Heroku

Prerequisites

  1. Install Docker
  2. Install Ruby
  3. gem install orats (see https://github.com/nickjj/orats)
  4. Install Heroku Toolbelt

Steps

# Terminal A in ~/dev
orats new herocker
cd herocker
git init && git add . && git commit -m "Scoffald from orats"
# Download puma.rb from this gist
curl -o config/puma.rb https://gist.githubusercontent.com/gabrieljoelc/8c04941042e9241a41b840cccf1ad5fb/raw/puma.rb
git add . && git commit -m "Split BIND_ON from PORT for puma binding"

# move port from BIND_ON to PORT
vi .env

docker-compose up --build

https://youtu.be/VBlFHuCzPgY

# Terminal B
cd ~/dev/herocker
git add . && git commit -m "Add Gemfile.lock"

# will have error because database isn't setup
open http://localhost:3000

docker-compose exec website rails db:create db:migrate

# should work locally now
open http://localhost:3000

git add . && git commit -m "Add schema file"

# Download Dockerfile.web from this gist
curl -O https://gist.githubusercontent.com/gabrieljoelc/8c04941042e9241a41b840cccf1ad5fb/raw/Dockerfile.web
git add . && git commit -m "Add production Dockfile"

# Creates a heroku app and adds heroku git remote
heroku create
heroku addons:create heroku-postgresql

# Manually set Rails/orats environment variables in Heroku app
heroku config:set \
SECRET_TOKEN=`docker-compose exec website rails secret` \
ACTION_CABLE_ALLOWED_REQUEST_ORIGINS=`heroku apps:info | grep "Web URL:" | sed -e 's/^Web URL:        //'` \
RAILS_SERVE_STATIC_FILES=true

# instead of doing `git push master heroku` like usual, we do the following:
heroku container:login

# This takes a while, so switch to discussing Heroku Docker docs
heroku container:push -R

https://youtu.be/VBlFHuCzPgY

# load schema into Heroku instance
heroku run rails db:schema:load

heroku open
FROM ruby:2.4.2-alpine
RUN apk update && apk add build-base nodejs postgresql-dev
# Configure the main working directory. This is the base
# directory used in any further RUN, COPY, and ENTRYPOINT
# commands.
RUN mkdir -p /app
WORKDIR /app
# Copy the Gemfile as well as the Gemfile.lock and install
# the RubyGems. This is a separate step so the dependencies
# will be cached unless changes to one of those two files
# are made.
COPY Gemfile Gemfile.lock ./
RUN bundle install --jobs 20 --retry 5 --without development test
# Set Rails to run in production
ENV RAILS_ENV production
ENV RACK_ENV production
ENV LOG_LEVEL warn
# make the repcompile task run successfully because config/application.rb assumes it has a value (invokes split)
ENV ACTION_CABLE_ALLOWED_REQUEST_ORIGINS https://dummy.com
ENV SECRET_TOKEN dummysecret
# Copy the main application.
COPY . ./
# Precompile Rails assets
RUN bundle exec rake assets:precompile
# Start puma
CMD bundle exec puma -C config/puma.rb
# Bind on a specific TCP address. We won't bother using unix sockets because
# nginx will be running in a different Docker container.
bind "tcp://#{ENV.fetch('BIND_ON')}
port ENV.fetch('PORT')
# Puma supports threading. Requests are served through an internal thread pool.
# Even on MRI, it is beneficial to leverage multiple threads because I/O
# operations do not lock the GIL. This typically requires more CPU resources.
#
# More threads will increase CPU load but will also increase throughput.
#
# Like anything this will heavily depend on the size of your instance and web
# application's demands. 5 is a relatively safe number, start here and increase
# it based on your app's demands.
#
# RAILS_MAX_THREADS will match the default thread size for Active Record.
threads_count = ENV.fetch('RAILS_MAX_THREADS') { 5 }.to_i
threads threads_count, threads_count
# Puma supports spawning multiple workers. It will fork out a process at the
# OS level to support concurrent requests. This typically requires more RAM.
#
# If you're looking to maximize performance you'll want to use as many workers
# as you can without starving your server of RAM.
#
# This value isn't really possible to auto-calculate if empty, so it defaults
# to 2 when it's not set. That is heavily leaning on the safe side.
#
# Ultimately you'll want to tweak this number for your instance size and web
# application's needs.
#
# If using threads and workers together, the concurrency of your application
# will be THREADS * WORKERS.
workers ENV.fetch('WEB_CONCURRENCY') { 2 }
# An internal health check to verify that workers have checked in to the master
# process within a specific time frame. If this time is exceeded, the worker
# will automatically be rebooted. Defaults to 60s.
#
# Under most situations you will not have to tweak this value, which is why it
# is coded into the config rather than being an environment variable.
worker_timeout 30
# The path to the puma binary without any arguments.
restart_command 'puma'
# Use the `preload_app!` method when specifying a `workers` number.
# This directive tells Puma to first boot the application and load code before
# forking the application. This takes advantage of Copy On Write process
# behavior so workers use less memory. If you use this option you need to make
# sure to reconnect any threads in the `on_worker_boot` block.
# preload_app!
# on_worker_boot do
# Since you'll likely use > 1 worker in production, we'll need to configure
# Puma to do a few things when a worker boots.
# We need to connect to the database. Pooling is handled automatically since
# we'll set the connection pool value in the DATABASE_URL later.
# defined?(ActiveRecord::Base) and ActiveRecord::Base.establish_connection
# end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment