LoginSignup
1
2

More than 1 year has passed since last update.

docker-composeでRails6の開発環境を構築(MySQL使用)

Last updated at Posted at 2022-05-09

Dockerで環境構築しってみか〜(ノリ)

「アプリを作るたび、ローカルに環境を構築するのが、面倒臭い」という怠惰な気持ちとやたら出てくるdocker仮想環境ってのがカッコよくて最近、Rails6とMySQLの開発環境を構築したのでまとめてみました。

Dockerとは?

こちらをご参照ください

0. Dockerをインストールしよう

まずは自分のパソコンにDockerをインストールします。
こちらからDockerをインストールしましょう。

 任意の作業フォルダを作成する

terminal
$ mkdir docker-test
$ cd docker-test

1. 必要なファイルを準備しよう

terminal
$ touch {Dockerfile,docker-compose.yml,Gemfile,Gemfile.lock,entrypoint.sh}

このこのコマンドを打てば、1発で必要なファイルを生成できます。

  • Dockerfile : Railsのコンテナにどんなものを用意するか記載します。
  • docker-compose.yml : RailsのコンテナとMySQLのコンテナを立ち上げる時の情報を記載します。
  • Gemfile : Railsで使用するgemの一覧を記載されます。
  • Gemfile.lock : Gemfileでインストールしたgemのバージョン情報などが書かれます。ファイルだけ用意し、編集はしません。

  
 次に作成したファイルを編集していきます。

 Dockerfileを編集する

FROM ruby:2.7.4

# yarnパッケージ管理ツールをインストール
RUN apt-get update && apt-get install -y curl apt-transport-https wget && \
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 && apt-get install -y yarn

RUN apt-get update -qq && apt-get install -y nodejs yarn
RUN mkdir /docker-test
WORKDIR /docker-test
COPY Gemfile /docker-test/Gemfile
COPY Gemfile.lock /docker-test/Gemfile.lock
RUN bundle install
COPY . /docker-test

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

# コンテナ起動時に実行させるスクリプトを追加
COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
EXPOSE 3000

# Rails サーバ起動
CMD ["rails", "server", "-b", "0.0.0.0"]

docker-compose.ymlを編集を編集する

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'"
    stdin_open: true
    tty: true
    volumes:
      - .:/docker-test
    ports:
      - "3000:3000"
    depends_on:
      - db

Gemfile の編集をする

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

entrypoint.sh の編集の編集をする

entrypoint.sh
#!/bin/bash
set -e

rm -f /myapp/tmp/pids/server.pid

exec "$@"

Gemfile.lockの中身は空で大丈夫です。

これで準備完了!

2. コンテナイメージをbuildしよう

作成したdocker関連のファイルを使用して、コンテナイメージをbuildしていきましょう。(数分かかるかも)

terminal
$ docker-compose build

3. rails newを実行してみよう!

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

docker-compose runコマンドでrails newを実行しましょう。

  • docker-compose : docker-composeを使用することを宣言します。
  • run : 以下のコマンドを実行します。
  • web : docker-compose.yml で名付けたコンテナ名としてwebを当てはめます。
  • rails new . : 現在のディレクトリで新しくrailsのプロジェクトを立ち上げます。
  • --force : 同じファイルがある場合、上書きします。
  • --no-deps : 開始しないサービスを指定します。
  • --database=mysql : データベースにMySQLを指定します。

このコマンドを実行することによって、railsの見慣れたファイルたちが生成されます。

4. データベースの準備をしましょう!

railsで使用するデータベースファイル(config/database.yml)の設定を編集していきます。
ファイル内の記述を書き換えましょう。

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

development:
  <<: *default
  database: docker-test_development
  host: db
  username: root
  password: password

test:
  <<: *default
  database: docker-test_test
  host: db
  username: root
  password: password

passwordは docker-compose.ymlで設定したものと合わせます。
hostの欄はdbにしましょう。これはdocker-compose.ymlで設定したMySQLのコンテナ名です。

次のコマンドを実行し、データベースを作成しましょう。

terminal
$ docker-compose run web rails db:create

↓実行後

エラー画像

あれ? エラーなんて聞いてないデスヨ、、、

Could not find public_suffix-4.0.6 in any of the sources
(訳:どのソースにもpublic_suffix-4.0.6が見つかりませんでした。
`bundle install`を実行して、不足しているgemをインストールしなさい。)

public_suffix-4.0.6というGemが足りないのかな?
大人しくいうことを聞いて、Gemfileに追記しました。

Gemfile
ruby '2.7.4'
+ gem 'public_suffix', '4.0.6'

再度、buildをします。(お時間!←マクドナルド風)

terminal
$ docker-compose build

再度、データベースを作成します。

terminal
$ docker-compose run web rails db:create

Created databaseと出ていれば、成功です!

5. webpackerをインストールしましょう!

Rails6からはwebpackerが必要になるので、webコンテナ内にインストールします。

terminal
$ docker-compose run web rails webpacker:install

Webpacker successfully installed 🎉 🍰(可愛い)

準備完了! お疲れ様です!

6. コンテナを立ち上げてみましょう!

terminal
$ docker-compose up -d

-d : -dはサーバーをバックグラウンドで起動することができます。

ブラウザを確認してみましょう!
http://localhost:3000/ でアクセスしてみましょう。
Yay! You're rails!

Railsでお馴染みの画面が出ていればdocker-composeでの構築が完了です!(一安心)

コンテナの停止

terminal
$ docker-compose down

一括でコンテナを削除し、サーバーを停止できます。
立ち上げたい時は、また $ docker-compose up -d を実行。

その他コマンド

terminal
$ docker-compose ps
起動しているコンテナを確認できます

$ docker-compose exec web rails g model (モデル名) (カラム名)
モデルの作成(コンテナが走っている状態でコマンドを打つ際はexecを入れてください)

$ docker-compose build
書き換えたGemfileを再度読み込む時も使用します

$ docker-compose exec web rails c
rails console を起動します

最後に

ここまでdocker-composeを使用したrails6の環境構築を記述してきました。
自分は、他サイトを参考にした際に、rubyのバージョンなどでエラーが出てしまい、思うように構築ができなかったため、
同じような現象が発生するかもしれません。その場合は自分の環境に合ったバージョンや設定で環境構築をお願いします。
また、間違いなどありましたらご指摘していただけますと幸いです。
最後まで読んでいただきありがとうございました。

参考

下記の記事を参考にさせていただきました。
ありがとうございます。

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