1
0

DockerでRailsとMySQLの開発環境を構築する方法

Posted at

はじめに

今回、Rails 7とMySQL 8.0の開発環境を構築する際にDockerを使用してみました。環境構築の手順を具体的に説明しながら、注意点やエラー解決の方法についての説明していきます。

必要なファイルの作成

まずは、以下のコマンドを使用して、必要なファイルを作成します。

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

このコマンドは、zshシェルを使用している場合にファイルを5つ一度に作成するためのものです。

Dockerfileの設定

次に、Dockerfileの内容を以下のように記述します。

# Rubyの公式イメージを使用
FROM ruby:3.2.5
ARG RUBYGEMS_VERSION=3.3.20

# 作業ディレクトリを指定
WORKDIR /myapp

# ホストのGemfileをコンテナ内にコピー
COPY Gemfile Gemfile.lock /myapp/

# bundle installを実行
RUN bundle install

# アプリケーションコードをコンテナ内にコピー
COPY . /myapp/

# entrypoint.shをコンテナ内にコピーし、実行権限を付与
COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh

# コンテナ起動時にentrypoint.shを実行
ENTRYPOINT ["/usr/bin/entrypoint.sh"]

# コンテナ起動時に実行するコマンドを指定
CMD ["rails", "server", "-b", "0.0.0.0"]

docker-compose.ymlの設定

次に、docker-compose.ymlファイルを以下のように設定します。

version: "3"
services:
  db:
    image: mysql:8.0
    environment:
      MYSQL_ROOT_PASSWORD: password
      MYSQL_DATABASE: mydatabase
    volumes:
      - mysql_volume:/var/lib/mysql
    ports:
      - "3306:3306"
    command: --default-authentication-plugin=mysql_native_password

  web:
    build:
      context: .
      dockerfile: Dockerfile
    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"
    stdin_open: true
    tty: true
    depends_on:
      - db

volumes:
  mysql_volume:

Gemfileの設定

次に、Gemfileをファイルを以下のように設定します。

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

entrypoint.shの設定

次に、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 "$@"

Railsプロジェクトの作成

以下のコマンドでRailsプロジェクトを作成します。

docker-compose run web rails new . --force --no-deps --database=mysql --css=tailwind --skip-jbuilder --skip-action-mailbox --skip-action-mailer --skip-test

オプションの説明

  • --database=mysql: MySQLを使用するために指定します
  • --css=tailwind: Tailwind CSSを使用する設定が自動で行われます
  • --skip-*: 必要のない機能をスキップします

MySQLの設定

config/database.ymlを以下のように編集します。

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

development:
  <<: *default
  database: myapp_development

test:
  <<: *default
  database: myapp_test

データベースの作成

次のコマンドでデータベースを作成します。

docker compose run web rails db:create

コンテナの起動

最後に、コンテナを起動させます。

docker compose up -d

ブラウザでhttp://localhost:3000/にアクセスし、Railsの初期画面が表示されれば成功です。

注意点

何度か試しているとキャッシュが残っていてうまく起動しないことがあります。
対処法①
以下のコマンドでdockerを再起動してみる

docker compose down -v
docker compose up -d

対処法②
volumes内のすべてのファイルとフォルダを強制的に削除する

docker-compose down
sudo rm -rf ./db/mysql/volumes/*
docker-compose up

参考文献

まとめ

私自身何度もエラーを出してしまい試行錯誤しながら環境構築しました。環境構築が完了した後もこの記事を書くために数回環境構築をしなおしてちゃんと動くか確認してみました。この記事が環境構築で困っている人の助けになれば嬉しいです。

1
0
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
1
0