Code Monkey home page Code Monkey logo

docker_rails_heroku's Introduction

Docker Rails Heroku

Template for building Rails application environment with Docker on Heroku.
Includes automatic test (RSpec) execution and deployment environment using CircleCI.

Stack

  • Rails 6.1.3 (+ Ruby 3.0.0)
  • PostgreSQL 13.2

Readying

$ docker pull ruby:3.0.0-alpine
$ docker pull postgres:13.2-alpine
$ git clone https://github.com/shifumin/docker_rails_heroku
$ cd docker_rails_heroku

Examples of Docker commands

# rails new
# Since .gitignore has been prepared in advance, add '--skip-git' option.
# For installing RSpec later, add '--skip-test' option.
$ docker-compose run --rm app rails new . --database=postgresql --skip-git --skip-test --skip-bundle

# After `rails new`, you need to replace 'config/database.yml'
$ cp database.yml.sample config/database.yml

# Add 'rspec-rails' gem to Gemfile and run 'bundle install'
$ vim Gemfile
$ docker-compose run --rm app bundle install

# Install RSpec
$ docker-compose run --rm app rails g rspec:install

# rails g
# the following is examples
$ docker-compose run --rm app rails g controller welcome index
$ docker-compose run --rm app rails g model user name:string

# rake db
$ docker-compose run --rm app rails db:create db:migrate

# rspec
$ docker-compose run --rm app rspec

# create and start containers
# boot the app (= `rails s`)
$ docker-compose up
# or docker-compose run --rm --service-ports app

# stop and remove containers
$ docker-compose down

# build or rebuild services
# (e.g.: after changing Gemfile or Dockerfile)
$ docker-compose build  # or `docker-compose up --build`

Deploy to Heroku

First, add the following environment variables to the build on CircleCI application.
And, register the GitHub repository on the CircleCI application.

# Put Dockerfile in app root to use Dockerfile with Heroku deploy
$ cp -i docker/web/Dockerfile .

CircleCI Environment Variables

HEROKU_AUTH_TOKEN=`heroku authorizations:create`
HEROKU_LOGIN='[email protected]'
HEROKU_API_KEY=`heroku authorizations:create`
HEROKU_APP_NAME='your_herokuapp_name'
# for development environment
HEROKU_APP_NAME_DEV='your_herokuapp_name_for_development'

Second, add the following environment valiables for Heroku on console.

Heroku Environment Variables

$ heroku config:add RACK_ENV=production
$ heroku config:add RAILS_SERVE_STATIC_FILES=enabled
$ heroku config:add RAILS_LOG_TO_STDOUT=enabled
# heroku config:add LANG=en_US.UTF-8
$ heroku config:add SECRET_KEY_BASE=$(docker-compose run --rm app rails secret)

Next, set up Heroku settings.

Heroku settings

# Install container plugin
$ heroku plugins:install heroku-container-registry

$ heroku container:login
$ heroku create
$ heroku container:push web

# attach postgresql addon
$ heroku addons:create heroku-postgresql:hobby-dev

$ heroku run rails db:migrate

Deploy to Heroku by CircleCI

After completing the above settings, execute deployment.
When git push origin master , CircleCI executes RSpec test and deploys docker container to heroku container registroy.

$ git push origin master

# Confirm application on browser.
$ heroku open

docker_rails_heroku's People

Contributors

shifumin avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

docker_rails_heroku's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.