rails6 docker mysql 環境構築

手順

各種ファイルの用意
rails new
イメージのビルド
database.yml の設定と DB 接続
コンテナ起動

各種バージョン

予め用意しておく

$ docker -v
Docker version 19.03.13, build 4484c46d9d
$ docker-compose -v
docker-compose version 1.27.4, build 40524192
$ rails -v
Rails 6.0.3.3
$ ruby -v
ruby 2.7.1p83 (2020-03-31 revision a0c7c23c9c) [x86_64-darwin19]
$ mysql --version
mysql  Ver 8.0.21 for osx10.15 on x86_64 (Homebrew)

ディレクトリ作成

必要なファイルを置くディレクトリを作成

$ mkdir myapp

ファイル作成

touch docker-compose.yml 
touch Gemfile
touch Gemfile.lock
touch Dockerfile
touch entrypoint.sh

Dockerfile中身

FROM ruby:2.7.1
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 /myapp
WORKDIR /myapp
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock
RUN bundle install
COPY . /myapp

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

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

参考
RUNでyarnを入れる
https://classic.yarnpkg.com/en/docs/install#debian-stable

docker-compose.yml

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

Gemfile

source 'https://rubygems.org'
gem 'rails', '~>6'

Gemfile.lock

空でおっけ

entrypoint.sh

#!/bin/bash
set -e

#Remove a potentially pre-existing server.pid for Rails.
rm -f /myapp/tmp/pids/server.pid

#Then exec the container's main process (what's set as CMD in the Dockerfile).
exec "$@"

参考
setコマンドの説明
【 set 】コマンド――シェルの設定を確認、変更する:Linux基本コマンドTips(205) - @IT

app作成

DBはmysql指定
あとでrspec入れたかったのでtestなし
webpacker入れる

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

rails new で各種ファイルの作成
webpacker のインストールが完了したら、Gemfile が更新されているので、イメージをビルド

$ docker-compose build

mysqlのエラーが出たので以下の手順を踏んだ(出なければ飛ばす)

ld: library not found for -lssl

opensslのパスがビルドの時に必要
ビルドのときにLDFLAGSとかCPPFLAGSにパスを追加する必要がある

$ bundle config --local build.mysql2 "--with-ldflags=-L/usr/local/opt/openssl/lib"
$ bundle install

参考
bundle installでmysql2がエラーになる件 - Qiita

config/database.ymlの編集

rails newで作成されてから

default: &default
  adapter: mysql2
  encoding: utf8mb4
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: <%= ENV.fetch("MYSQL_USERNAME", "root") %>
  password: <%= ENV.fetch("MYSQL_PASSWORD", "password") %>
  host: <%= ENV.fetch("MYSQL_HOST", "db") %>

development:
  <<: *default
  database: myapp_development

test:
  <<: *default
  database: myapp_test

production:
  <<: *default
  database: myapp_production
  username: myapp
  password: <%= ENV['MYAPP_DATABASE_PASSWORD'] %>

DB 作成

$ docker-compose run web rake db:create

問題なければいまくいくはず

buildエラーCould not find bindex-0.8.1 in any of the sourcesが出た場合

キャッシュなしでbuildしなおしてからDB作成
10分くらいかかったと思う

$ docker-compose build --no-cache

参考
Dockerでコンテナ内にbundle installされない問題の解決法 - カクカクしかじか docker をキャッシュを使わないでビルドする | ハックノート

コンテナ起動

$ docker-compose up

localhost:3000 にアクセス