LoginSignup
2
3

More than 3 years have passed since last update.

Docker ComposeでRuby2.7+Rails6.0+MySQL8.0の環境を用意してrails newする

Last updated at Posted at 2020-05-06

下記を参考に、より簡易的な Docker 環境で rails new から rails server が起動するまでの流れをまとめます。

クジラに乗ったRuby: Evil Martians流Docker+Ruby/Rails開発環境構築(翻訳)
https://techracho.bpsinc.jp/hachi8833/2019_09_06/79035

※ DB に MySQL を使っている点や、rails new (アプリケーション作成時) から始める点、設定が簡易的になっている点がリンク先とは違います。

作業ディレクトリ用意

terminal
$ mkdir myapp
$ cd myapp

Docker 関連ファイルの作成

ディレクトリ構造
myapp/
├── Dockerfile
└── docker-compose.yml
docker-compose.yml
version: '3.7'

services:
  backend:
    build: .
    stdin_open: true
    tty: true
    command: bash -c "rm -f tmp/pids/server.pid && rails s -b '0.0.0.0'"
    environment:
      - DATABASE_URL=mysql2://root:@mysql:3306
      - DEFAULT_HOST=localhost:8030
    volumes:
      - .:/app:cached
      - bundle:/bundle
      - node_modules:/app/node_modules
    depends_on:
      - mysql
    ports:
      - '8030:3000'
  mysql:
    image: mysql:8.0
    command: --default-authentication-plugin=mysql_native_password
    environment:
      - MYSQL_ALLOW_EMPTY_PASSWORD=1
    volumes:
      - mysql_data:/var/lib/mysql

volumes:
  mysql_data:
  bundle:
  node_modules:
Dockerfile
FROM ruby:2.7.0

ENV LANG ja_JP.utf8
ENV GEM_HOME /bundle
ENV BUNDLE_JOBS 4
ENV BUNDLE_RETRY 3
ENV BUNDLE_PATH $GEM_HOME
ENV BUNDLE_APP_CONFIG $BUNDLE_PATH
ENV BUNDLE_BIN $BUNDLE_PATH/bin
ENV PATH /app/bin:$BUNDLE_BIN:$PATH

RUN set -x && curl -sL https://deb.nodesource.com/setup_14.x | bash -

RUN set -x && curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - && \
    echo 'deb http://dl.yarnpkg.com/debian/ stable main' > /etc/apt/sources.list.d/yarn.list

RUN set -x && apt-get update -qq && \
  apt-get install -yq build-essential nodejs yarn vim default-mysql-client

RUN set -x && gem update --system && gem install bundler:2.1.2

RUN mkdir -p /app
WORKDIR /app

コンテナの起動から MySQL 接続確認

terminal
# イメージのビルド
$ docker-compose build

# コンテナを起動し、Bash にログイン
$ docker-compose run --rm backend bash

# MySQL に接続し、バージョンが確認できるか (少し待つ)
> mysql -uroot -hmysql -e 'select version();'
+-----------+
| version() |
+-----------+
| 8.0.20    |
+-----------+

# コンテナからログアウト
> exit

Rails アプリケーションの生成

terminal
# コンテナを起動し、Bash にログイン (service-ports オプション付)
$ docker-compose run --rm --service-ports backend bash

Rails のインストール

terminal
# Gemfile 作成
> bundle init

# Rails のインストール
> bundle add rails --version "~> 6.0"

Rails アプリケーションの生成

terminal
# Rails アプリケーションの生成
> rails new . -d mysql --skip-test --force

# 依存ライブラリのインストール
> bundle
> yarn

database.yml の username / password / host をコメントアウトして、url を追加する

config/database.yml
# ...

default: &default
  adapter: mysql2
  encoding: utf8mb4
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  url: <%= ENV['DATABASE_URL'] %>
  # username: root
  # password:
  # host: localhost

# ...

データベースのセットアップ

terminal
# データベースの作成
> rails db:create

# マイグレーション
> rails db:migrate

動作確認

terminal
# Rails サーバ起動
> rails s -b "0.0.0.0"

http://localhost:8030 で確認

terminal
# コンテナからログアウト
> exit

# 次回からは下記で起動可能
$ docker-compose up

http://localhost:8030 で確認

終了する

terminal
# コンテナ停止
$ docker-compose down

# コンテナ停止、volumes の削除
# $ docker-compose down --volumes

以上です。

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