web_bonsaiの日記

web開発の学習日記です。誰に見せるためでもないただの日記です。

Docker Composeを使ってローカルでRails6のスタートページを表示するところまでやってみる | Mac + Docker + Rails その0004

参考にさせていただいたページ

今日の環境

参考ページについて

基本的には公式の「Quickstart: Compose and Rails | Docker Documentation」に倣ってやっていきますが、rubyrailsのバージョンを新しめのものにして環境構築していきます。

プロジェクトディレクトリを作成して移動

ディレクトリ名は何でも良いです。

このあと作成していく設定ファイルの記述内容にも影響しません。

mkdir rails_sample
cd rails_sample

Dockerfileの作成とその記述内容

vim Dockerfile

2022年05月13日現在で最新の安定版はruby3.1.2らしく、docker hubにも ruby:3.1.2 があることが確認できたので、それを使おうと思います。

docker-compose + rails6 環境構築」のページを参考にRails6に合うように記述します。

# syntax=docker/dockerfile:1
FROM ruby:3.1.2

## nodejsとyarnはwebpackをインストールする際に必要
# yarnパッケージ管理ツールをインストール
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

RUN apt-get update -qq && apt-get install -y nodejs build-essential libpq-dev postgresql-client yarn vim
WORKDIR /myapp
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock
RUN bundle install

RUN yarn install --check-files
RUN bundle exec rails webpacker:install

# 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

参考ページに記載されているものから、バージョンを上げてrails6にしてみます。

以下の通り記述します。

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

空の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が更新されます。

正常に完了したようですが、以下のようなメッセージが表示されているのでちゃんと読んでおくと良さそうです。

hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint: 
hint:   git config --global init.defaultBranch <name>
hint: 
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint: 
hint:   git branch -m <name>
You don't have net-smtp installed in your application. Please add it to your Gemfile and run bundle install
Yarn not installed. Please download and install Yarn from https://yarnpkg.com/lang/en/docs/install/

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

webpacker.ymlが無いのでexited with code 1になった

exited with code 1 になってしまったので、よく読むと以下のようなメッセージが出力されていました。

webpacker.ymlが無いので rails webpacker:install を実行してくださいと書いてありました。

rails6_sample-web-1  | /usr/local/bundle/gems/webpacker-5.4.3/lib/webpacker/configuration.rb:103:in `rescue in load': Webpacker configuration file not found /myapp/config/webpacker.yml. Please run rails webpacker:install Error: No such file or directory @ rb_sysopen - /myapp/config/webpacker.yml (RuntimeError)
rails6_sample-web-1  | Exiting
rails6_sample-web-1  | /usr/local/lib/ruby/3.1.0/psych.rb:670:in `initialize': No such file or directory @ rb_sysopen - /myapp/config/webpacker.yml (Errno::ENOENT)

webpackerをインストールする

以下のコマンドを実行しました。

docker-compose run web rails webpacker:install

localhost:3000にアクセスしてみる

ActiveRecord::NoDatabaseError が表示されました。

db createする

参考ページに倣って、以下のコマンドでDBを作成します。

docker-compose run web rails db:create

docker-compose upし直す。

docker-compose up

localhost:3000にアクセスし直す

http://localhost:3000/」にアクセスしたら、railsのスタートページが表示されました。

rails7の環境構築にも挑戦してみようかと思います。