参考にさせていただいたページ
今日の環境
参考ページについて
基本的には公式の「Quickstart: Compose and Rails | Docker Documentation」に倣ってやっていきますが、rails7に対応するために「Rails 7 + MySQLの環境構築をDocker composeで作る - Qiita」を参考にやっていこうと思います。
プロジェクトディレクトリを作成して移動
ディレクトリ名は何でも良いです。
このあと作成していく設定ファイルの記述内容にも影響しません。
mkdir rails_sample
cd rails_sample
Dockerfileの作成とその記述内容
vim Dockerfile
2022年05月13日現在で最新の安定版はruby3.1.2らしく、docker hubにも ruby:3.1.2 があることが確認できたので、それを使おうと思います。
「Rails 7 + MySQLの環境構築をDocker composeで作る - Qiita」も参考にしつつ、以下のように記述しました。
FROM ruby:3.1.2 RUN apt-get update -qq && apt-get install -y nodejs postgresql-client vim WORKDIR /myapp COPY Gemfile /myapp/Gemfile COPY Gemfile.lock /myapp/Gemfile.lock 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 # Configure the main process to run when running the image CMD ["rails", "server", "-b", "0.0.0.0"]
Gemfileの作成とその記述内容
vim Gemfile
参考ページに記載されているものから、バージョンを上げてrails7にしてみます。
以下の通り記述します。
source 'https://rubygems.org' gem 'rails', '~>7'
空のGemfile.lockを作成
dockerコンテナの中にコピーして使うので空ファイルを作成しておきます。
touch Gemfile.lock
entrypoint.shの作成とその記述内容
以下の説明の通りですが、 server.pid というファイルが削除されずにrailsサーバーが起動できなくなったりするので、entrypoint.sh というファイルを作成して、server.pidを削除するコマンドを記述します。
Next, provide an entrypoint script to fix a Rails-specific issue that prevents the server from restarting when a certain server.pid file pre-exists. This script will be executed every time the container gets started. entrypoint.sh consists of:
エディタは何でも良いですが、vimで。
vim 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の作成とその記述内容
vim docker-compose.yml
version: "3.9" services: db: image: postgres volumes: - ./tmp/db:/var/lib/postgresql/data environment: POSTGRES_PASSWORD: password 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
ここまでやった段階でのディレクトリ構造とファイル構成
最初に作成したrails_sampleディレクトリに、以下のようにファイルがあるだけです。
. ├── Dockerfile ├── Gemfile ├── Gemfile.lock ├── docker-compose.yml └── entrypoint.sh
rails newする
参考ページに倣って、以下のコマンドを実行します。--rm
だけ追加しています。
docker-compose run --rm --no-deps web rails new . --force --database=postgresql
rails newされて、railsのプロジェクトが生成されます。
このときGemfileやGemfile.lockが更新されます。
以下のようなエラーが赤字で出力されました。
Could not find gem 'sprockets-rails' in locally installed gems. rails importmap:install Could not find gem 'sprockets-rails' in locally installed gems. Run `bundle install` to install missing gems. rails turbo:install stimulus:install Could not find gem 'sprockets-rails' in locally installed gems. Run `bundle install` to install missing gems.
bundle installする
以下のコマンドを実行します。
docker-compose run web bundle install
白字なのでエラーではなさそうですが以下のメッセージが表示されました。
RubyZip 3.0 is coming! ********************** The public API of some Rubyzip classes has been modernized to use named parameters for optional arguments. Please check your usage of the following classes: * `Zip::File` * `Zip::Entry` * `Zip::InputStream` * `Zip::OutputStream` Please ensure that your Gemfiles and .gemspecs are suitably restrictive to avoid an unexpected breakage when 3.0 is released (e.g. ~> 2.3.0). See https://github.com/rubyzip/rubyzip for details. The Changelog also lists other enhancements and bugfixes that have been implemented since version 2.3.0.
docker-compose buildする
参考ページに倣って以下のコマンドを実行します。
docker-compose build
railsのデータベース設定をする
参考ページに倣って、config/database.ymlを以下の通りになるように編集します。
default: &default adapter: postgresql encoding: unicode host: db username: postgres password: password pool: 5 development: <<: *default database: myapp_development test: <<: *default database: myapp_test
たぶんdefaultの host, username, password の項目を追記するだけだと思います。
docker-compose upする
参考ページに倣って、以下のコマンドで、webとdbのサーバーを起動します。
docker-compose up
localhost:3000にアクセスしてみる
ActiveRecord::NoDatabaseError が表示されました。
db createする
docker-compose stopはしなくても良いので、参考ページに倣って、別タブで以下のコマンドを実行してDBを作成します。
docker-compose run web rails db:create
localhost:3000にアクセスし直す
「http://localhost:3000/」にアクセスしたら、railsのスタートページが表示されました。