LoginSignup
0
0

More than 3 years have passed since last update.

Docker上のRailsをherokuへデプロイする

Posted at

herokuへデプロイする練習

Dcokerコンテナ上に、Railsアプリを組んでCircleciを使って自動デプロイまでを一気にやろうとしたら
迷宮入りしたので、原点回帰しようと思います。(第2段)

自分の環境

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

開発環境は、MySQLで
本番環境は、PostgreSQLというパターンで組んでみようと思います。

手元のDocker上にRailsアプリを用意する

まずは、アプリを作るディレクトリを作成し、そこにtouchコマンドで必要な諸々を用意します。

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 "$@"

上記5点を用意できれば、下記コマンドを実行しRailsアプリを作成します。

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

併せて、テキトーな中身を作っておきます。

$ 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

herokuへデプロイする準備

続いて、Railsアプリをherokuにデプロイする前に、本番環境用にpostgreSQLを用意します。
- config/database.ymlの設定
- Gemfile にpgを追加
- config/enviroments/deviropment.rbの設定

config/database.ymlの設定

config/database.yml
production:
  <<: *default
  adapter: postgresql
  encoding: unicode
  pool: 5

Gemfileの設定

本番環境用にgemファイルを用意します。
productionのグループにpgを追加します。
また、MySQLは開発環境用として扱う様にするために、group :development, :test doの中へ移動させます。

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

config/enviroments/deviropment.rbの設定

Rails6特有ですが、DNS離バインディング攻撃からの保護が入っているらしく、
hostを入れてあげる必要があります。

config/enviroments/deviropment.rb
config.hosts << "radiant-springs-45017.herokuapp.com"

下記記事を参考にさせていただきました。
https://qiita.com/kodai_0122/items/67c6d390f18698950440

編集を終えたら、ビルドします。

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

あとは、コマンド打って、ルンルンとherokuへデプロイします。

$ docker-compose down #一度落としておかないとエラーになる可能性があるとのこと
$ heroku login
$ heroku create アプリ名もしくは空欄
$ 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

終わり!

割愛しましたが、herokuへデプロイした際にエラーが発生しました。
その際は、ターミナル上で$ heroku logs --tail等を打ち、エラーを調べて解決させました。
成功された記事を参考した場合でも、環境の違い等でエラーが発生しうるので、理解のために都度調べる癖をつけるのは大事だと思った次第です。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0