4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

DockerによるRuby on Railsの環境構築手順

Last updated at Posted at 2019-04-10

だいぶ、手間どりましたが、以下のリンク先が一番わかりやすかったので、今後のためにも、まとめたいと思います。
※リンク先の方は丁寧親切です。
https://remonote.jp/docker-mac-rails-postgresql

任意の作成したディレクトリー配下に以下の4つのファイルを作成

・ dockerfile
・ Gemfile
・ Gemfile.lock →空のファイルで良い
・ docker-compose.yml

dockerfile
FROM ruby:2.6.2
 
ENV LANG C.UTF-8
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs
 
RUN mkdir /blog_app_sample
WORKDIR /blog_app_sample
ADD Gemfile /blog_app_sample/Gemfile
ADD Gemfile.lock /blog_app_sample/Gemfile.lock
RUN bundle install
COPY . /blog_app_sample

・今回、blog_app_sampleというアプリを作成する前提。
・rubyのバージョン指定は任意

Gemfile
source 'https://rubygems.org'
gem 'rails'
# gem 'rails', '~> 5.2.3'でも良い
docker-compose.yml
version: '3'
services:
  db:
    image: postgres
    volumes:
      - ./tmp/db:/var/lib/postgresql/data
  web:
    build: .
    command: bundle exec rails s -p 3000 -b '0.0.0.0'
    volumes:
      - .:/myapp
    ports:
      - "3000:3000"
    depends_on:
      - db

conposeのバージョンがあるみたい。
また、ymlなので、インデントには注意する。

アプリケーションの生成

$ docker-compose run web rails new . --force --database=postgresql

Dockerのイメージ構築

$ docker-compose build

config/database.ymlの設定

config/database.yml
default: &default
  adapter: postgresql
  encoding: unicode
  host: db
  username: postgres
  password:
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

development:
  <<: *default
  database: blog_app_sample_development

test:
  <<: *default
  database: blog_app_sample_test

production:
  <<: *default
  database: blog_app_sample_production
  username: blog_app_sample
  password: <%= ENV['BLOG_APP_SAMPLE_DATABASE_PASSWORD'] %>

defaultのところを追記する

Dockerの起動

$ docker-compose up

これでlocalhost:3000にアクセスすると、いつもの画面が表れる。
※停止は、Ctrl+C

pidの削除はこれ↓

$ rm tmp/pids/server.pid

コマンド実行例

$ docker-compose run web rails g scaffold Blog title:string content:text

$ docker-compose run webが頭につく。

Gemfileを編集した時

$ docker-compose run web bundle install
$ docker-compose build

再度、イメージ構築をしないといけない。

https://qiita.com/fuku_tech/items/dc6b568f7f34df10cae7
https://qiita.com/kenzoukenzou104809/items/90dd187a3fb1c8897656

Herokuへのデプロイ

herokuでのデプロイ手順
# ローカルでWebサーバーが動作している(pidsが存在する)と、herokuでWebサーバーが起動しない可能性があるので、一旦ローカルのサーバーを停止しておきます。また server.pid が存在する場合は消しておきます
$ docker-compose down
$ rm tmp/pids/server.pid

$ heroku container:login
$ heroku login 

$ heroku create

$ heroku container:push web

$ heroku addons:create heroku-postgresql:hobby-dev

# このコマンドは、念のため実行
$ heroku run rails db:migrate

$ heroku container:release web

この状態で、「git push heroku master」とすると
恐れく、heroku.ymlが無いとエラーがでるので、
以下のようなheroku.ymlを作成し、「git push heroku master」する

/heroku.ymlを作成する
build:
  languages:
    - ruby
release:
  command:
    - rake db:migrate
run:
  web: bundle exec puma -C config/puma.rb

https://ginpen.com/2018/08/07/docker-rails-example/
https://qiita.com/okyk/items/a374ddb3f853d1688820

スクリーンショット 2019-08-09 19.13.06.png

4
3
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
4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?