LoginSignup
76
61

More than 1 year has passed since last update.

Rails 7 + MySQLの環境構築をDocker composeで作る

Last updated at Posted at 2022-02-13

二年前にRails 6 + MySQLの環境構築をDocker composeで作るという投稿を書いたが、Rails 7ではWebpackerが廃止されるなど色々と変更点があったので、Rails 7用に書き直した。


Dockerの公式サイトにはRailsアプリケーション用のDocker composeチュートリアルがあるが、少し情報が古くRails 7ではうまく動かなかったので、Rails 7で動かすための方法を載せておく。基本は公式チュートリアルの手順に従っているため、Rails 7用に変更したところを中心に補足を入れている。

DBはPostgresではなくMySQLを使う方法を載せておく。

プロジェクトディレクトリの準備

mkdir myrailsapp
cd myrailsapp

設定ファイルの準備

Dockerfile
FROM ruby:3.1

RUN mkdir /myapp
WORKDIR /myapp
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock

# Bundlerの不具合対策(1)
RUN gem update --system
RUN bundle update --bundler

RUN bundle install
COPY . /myapp

# Add a script to be executed every time the container starts.
COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
EXPOSE 3000

# Start the main process.
CMD ["rails", "server", "-b", "0.0.0.0"]

2022.10.08追記:
(1) Bundler 2.3.7の不具合回避のため、bundle installを実行する前にBundlerを最新にするためのコードを加えた。

Rails 7ではWebpackerが標準では組み込まれなくなったので、yarnやnodejsのインストールが不要になった。Dockerfileからも当該の部分を削除した。

postgresは使用しないため除外。

なお、上の公式チュートリアルのDockerfileではroot権限でRailsサーバーが実行される形になっているので、そこは適宜ユーザを作って実行する形に変更した方が良いかもしれない。

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

GemfileでRails 7系を指定。このGemfileはRailsプロジェクト作成後にRailsプロジェクトの内容で上書きされることになる。Rails 7.0.0 + Ruby 3.1だとnet/smtpに関する問題が発生するので、必ず7.0.1以降を指定する。

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

この辺りは公式のサンプルのままなので説明省略。

docker-compose.yml
version: '3'
services:
  db:
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD: password
      MYSQL_DATABASE: root
    ports:
      - "3306:3306"
    volumes:
      - ./tmp/db:/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

MySQLを使う形に設定を変更。

Railsプロジェクトの作成


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

--database=mysqlでDB設定をMySQLにした状態でプロジェクト作成。

なお、Railsのバージョンによってはsassc 2.4.0が依存ライブラリとして指定されるが、sassc 2.2.0以降はWindows以外の環境でのインストールに膨大な時間がかかるため、問題なければ2.1系に下げることでbundle installのスピードを高速化できる。https://qiita.com/croquette0212/items/d2f48f30c3ed7dcd0e3c

docker-compose build

DB接続設定

database.yml
development:
  <<: *default
  database: myapp_development
  host: db
  username: root
  password: password

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  <<: *default
  database: myapp_test
  host: db
  username: root
  password: password

developmenttestがdocker-composeで起動したMySQLイメージdbを使うように設定を記述。

DB作成

docker-compose run web rails db:create

イメージの起動

docker-compose up

http://localhost:3000/ にアクセスしてページが表示されたら成功。

76
61
2

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
76
61