Deploy Rails on Docker to heroku

Practice deploying to heroku

If you build a Rails application on a Dcoker container and use Circleci to do automatic deployment at once Now that I've entered the labyrinth, I'm going to return to the origin. (2nd)

My environment

Ruby : 2.6.6 rails : 6.0.3.2 git : 2.23.0 heroku-cli : 7.42.13 darwin-x64 node-v12.16.2 Docker : 19.03.12

Development environment is MySQL I will try to build a production environment with a pattern called PostgreSQL.

Prepare a Rails application on your Docker

First, create a directory to create the application, and prepare various necessary items with the touch command there.

terminal


$ touch {Dockerfile,docker-compose.yml,Gemfile,Gemfile.lock,entrypoint.sh}

Dockerfile


FROM ruby:2.6

RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - \
    && echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list \
    && apt-get update -qq \
    && apt-get install -y nodejs yarn \
    && mkdir /heroku_app
WORKDIR /heroku_app

COPY Gemfile //Gemfile
COPY Gemfile.lock /heroku_app/Gemfile.lock
RUN bundle install
COPY . /heroku_app

COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
EXPOSE 3006

CMD ["rails", "server", "-b", "0.0.0.0"]

docker-compose.yml


version: '3'
services:
  db:
    image: mysql:8.0
    environment:
      MYSQL_ROOT_PASSWORD: password
    ports:
      - '3306:3306'
    command: --default-authentication-plugin=mysql_native_password
    volumes:
      - mysql-data:/var/lib/mysql:cached
  web:
    build: .
    command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3006 -b '0.0.0.0'"
    volumes:
      - .:/heroku_app
    ports:
      - "3006:3000"
    depends_on:
      - db
    stdin_open: true
    tty: true
    command: bundle exec rails server -b 0.0.0.0
volumes:
  mysql-data:
    driver: local

Gemfile


source 'https://rubygems.org'
rails ‘6.0.3’

entrypoint.sh


#!/bin/bash
set -e
# Remove a potentially pre-existing server.pid for Rails.
rm -f /heroku_app/tmp/pids/server.pid
# Then exec the container's main process (what's set as CMD in the Dockerfile).
exec "$@"

If you can prepare the above 5 points, execute the following command to create a Rails application.

terminal


$docker-compose run web rails new . --force --no-deps --database=mysql

At the same time, make a textual content.

$ docker-compose run web rails g scaffold blog title:string body:text
$ docker-compose run web rails db:migrate
$ docker-compose up -d

スクリーンショット 2020-09-09 0.00.04.png

Ready to deploy to heroku

Next, prepare postgreSQL for your production environment before deploying your Rails app to heroku. --config / database.yml settings --Add pg to Gemfile --Settings in config / environments / deviropment.rb

config / database.yml settings

config/database.yml


production:
  <<: *default
  adapter: postgresql
  encoding: unicode
  pool: 5

Gemfile settings

Prepare the gem file for the production environment. Add pg to the production group. Also, move MySQL into `group: development,: test do` so that it will be treated as a development environment.

Gemfile


group :production do
  gem 'pg', '~> 0.19.0'
end

Settings in config / environments / deviropment.rb

It is peculiar to Rails6, but it seems that it has protection from DNS detachment binding attacks, You need to put in the host.

config/enviroments/deviropment.rb


config.hosts << "radiant-springs-45017.herokuapp.com"

I referred to the following article. https://qiita.com/kodai_0122/items/67c6d390f18698950440

After editing, build.

terminal


docker-compose build
$docker-compose run web rails db:create
$docker-compose up -d

After that, type a command and deploy to Runrun and heroku.

$ docker-compose down #If you do not drop it once, an error may occur.
$ heroku login
$heroku create app name or blank
$ heroku container:login
$ heroku container:push web
$ heroku adding:create heroku-postgresql:hobby-dev
$ heroku container:release web
$ heroku open

スクリーンショット 2020-09-09 10.44.50.png

the end!

I omitted it, but an error occurred when deploying to heroku. In that case, I typed $ heroku logs --tail etc. on the terminal to check the error and solve it. Even if you refer to a successful article, an error may occur due to differences in the environment, so I thought it was important to get into the habit of checking each time for understanding.

Recommended Posts

Deploy Rails on Docker to heroku
Deploy to Heroku [Ruby on Rails] Beginner
How to deploy on heroku
How to deploy jQuery on Rails
How to deploy Bootstrap on Rails
Deploy Flask's Docker image on Heroku
heroku: docker: deploy
Deploy your Rails app on Heroku
Steps to deploy to Heroku
Rails deploy with Docker
Deploy RAILS on EC2
Deploy to Ruby on Rails Elastic beanstalk (EB deploy)
Introducing New Relic to Rails apps on Heroku
[Rails MySQL] How to reset DB on heroku
Deploy Line bot with rails5 + Docker + Heroku Note
Deploy Vapor Project to Heroku
How to deploy to Heroku from a local docker image
Deploy to Ruby on Rails Elastic beanstalk (Environment construction)
Deploy to Ruby on Rails Elastic beanstalk (IAM permission change)
How to deploy Laravel on CentOS 7
Steps to run docker on Mac
Run puppeteer-core on Heroku (Docker edition)
How to deploy a simple Java Servlet app on Heroku
How to use Ruby on Rails
heroku deploy
To beginners launching Docker on AWS
How to run JavaFX on Docker
Deploy a war file on Heroku
Rails on Tiles (how to write)
Rails on Docker environment construction procedure
How to deploy a Rails application on AWS (article summary)
[Rails / Heroku / MySQL] How to reset the DB of Rails application on Heroku
Redirect to your own domain with rack-rewrite on Heroku x Rails
I was able to deploy the Docker + laravel + MySQL app to Heroku!
[Ruby on Rails] How to use CarrierWave
Deploy heroku with Rails6 (Cloud9 + Ubuntu) + MySQL
Run Embulk on Docker to convert files
[Rails] How to use rails console with docker
I want to display background-ground-image on heroku.
Migrate existing Rails 6 apps to Docker environment
How to publish an application on Heroku
Try deploying Rails app to EC2-Part 2 (Deploy)-
Deploy a Java web app on Heroku
[Ruby on Rails] How to use redirect_to
[Ruby on Rails] How to use kaminari
[Ruby on Rails] Button to return to top
Deploy a Tomcat-based Eclipse project on Heroku
How to build Rails 6 environment with Docker
Liberty on Docker
How to build a Ruby on Rails environment using Docker (for Docker beginners)
Enable Docker build cache on GitHub Action and deploy to Amazon ECS
How to deploy
How to build a Ruby on Rails development environment with Docker (Rails 6.x)
Rails Docker ~ Part 1 ~
Rails Docker ~ Part 2 ~
How to build a Ruby on Rails development environment with Docker (Rails 5.x)
Ruby on Rails ✕ Docker ✕ MySQL Introducing Docker and docker-compose to apps under development
Redmine on Docker
[Ruby on Rails] How to display error messages
How to deploy a container on AWS Lambda
Settings to bypass Docker Hub restrictions on CircleCI