LoginSignup
14
8

More than 3 years have passed since last update.

docker-composeで『Everyday Rails - RSpecによるRailsテスト入門』 の環境構築

Last updated at Posted at 2019-08-07

はじめに

Everyday Rails の作業をローカル環境で実施するために、
docker-composeを用いて環境構築をしました。その方法をメモします。DBはmysql2を使用しました。

リモートリポジトリからのクローン、作業ディレクトリ作成

リモートリポジトリからクローンする

git clone https://github.com/everydayrails/everydayrails-rspec-2017.git

ディレクトリを移動する

cd everydayrails-rspec-2017

ファイル作成、修正

作業ディレクトリ内に、以下のファイルを新たに作成する
- Dockerfile
- docker-compose.yml

Dockerfile

FROM ruby:2.3.7
ENV LANG C.UTF-8

RUN apt-get update -qq && apt-get install -y \
    build-essential \
    nodejs \
 && rm -rf /var/lib/apt/lists/*

RUN gem install bundler

WORKDIR /tmp
ADD Gemfile Gemfile
ADD Gemfile.lock Gemfile.lock
RUN bundle install

ENV APP_HOME /myapp
RUN mkdir -p $APP_HOME
WORKDIR $APP_HOME
ADD . $APP_HOME

docker-compose.yml

version: '3'
services:
  web:
    build: .
    ports:
      - "3000:3000"
    command: bundle exec rails s -p 3000 -b '0.0.0.0'
    volumes:
      - .:/myapp
      - bundle:/usr/local/bundle
    depends_on:
      - db
  db:
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD: password
    ports:
      - '3306:3306'
    volumes:
      - mysql_data:/var/lib/mysql
volumes:
  bundle:
  mysql_data:

DBをmysql2に変更するため、database.ymlを修正

config/database.yml

default: &default
  adapter: mysql2
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  timeout: 5000
  username: root
  password: password   # docker-compose.ymlのMYSQL_ROOT_PASSWORDで指定したもの
  host: db # docker-compose.ymlのサービス名

development:
  <<: *default
  database: development_database

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  <<: *default
  database: test_database

production:
  <<: *default
  database: production_database

sqlite3の代わりにmysql2をインストールするため、Gemfileの該当箇所を修正

Gemfile

#gem 'sqlite3'
gem 'mysql2'

docker-composeの起動

dockerイメージをビルドする。

$ docker-compose build 

bundle installを実行する。

docker-compose run web bundle install

コンテナを起動し、開発環境を構築する。

$ docker-compose up 

ここで、

/usr/local/bundle/gems/activerecord-5.1.1/lib/active_record/connection_adapters/connection_specification.rb:188:in `rescue in spec': Specified 'mysql2' for database adapter, but the gem is not loaded. Add `gem 'mysql2'` to your Gemfile (and ensure its version is at the minimum required by ActiveRecord). (Gem::LoadError)

というエラーが出た。以下の参考記事に従って、Gemfile内のmysql2のバージョンを変更
参考記事:"Specified 'mysql2' for database adapter, but the gem is not loaded" when upgrading to 0.5.0

Gemfile

gem 'mysql2', '~> 0.4.0'

再度dockerを起動し直したら、エラーが無くなった。

DBの作成

以下のコマンドでDBを新たに作成する。

docker-compose exec web rails db:create
docker-compose exec web rails db:migrate

ブラウザでlocalhost:3000にアクセスし、メイン画面が出てきたら成功

14
8
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
14
8