Lチカ開発ブログ

https://l-chika.com/の開発ブログ

Docker + Rails5.2.0.beta2

概要

DockerでRails環境構築 - DBはMySQL

参考

構築環境

  • Ruby 2.4.2
  • Rails 5.2.0.beta2
  • Docker 17.09.1
  • MySQL 5.7
  • Node 8.9.3
  • Yarn 1.3.2

step

$ mkdir {APP_DIR} && cd $_

Dockerfile

$ vim Dockerfile
FROM ruby:2.4.2
ENV TZ=Asia/Tokyo
ENV LANG C.UTF-8

# NOTE: Yarn requires Node.js 4.0 or higher to be installed.
RUN curl -sL https://deb.nodesource.com/setup_8.x | bash - && apt-get install -y nodejs

RUN apt-get update -qq && apt-get install -y build-essential libmysqlclient-dev vim

ARG APP_DIR="/myapp"
RUN mkdir $APP_DIR
WORKDIR $APP_DIR

ADD Gemfile ${APP_DIR}/Gemfile
ADD Gemfile.lock ${APP_DIR}/Gemfile.lock

ENV BUNDLE_GEMFILE=${APP_DIR}/Gemfile \
  BUNDLE_JOBS=2 \
  BUNDLE_PATH=/bundle
RUN gem install bundler & bundle install

ADD . $APP_DIR

Gemfile

$ touch Gemfile.lock
$ vim Gemfile
ruby '2.4.2'

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

docker-compose

$ vim docker-compose.yml

docker-compose.yml

  • volumes/myappを環境に合わせて適宜変更
  • MySQL
    • 外部からも接続できるようにする
    • データも永続化する
    • 文字コードを指定
version: '3'
services:
  db:
    image: mysql:5.7
    command: "mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci"
    environment:
      MYSQL_ROOT_PASSWORD: password
    volumes:
      - mysql-db:/var/lib/mysql
    ports:
      - "3306:3306"
  web:
    build: .
    command: bundle exec rails s -p 3000 -b '0.0.0.0'
    environment:
      EDITOR: vim
    volumes:
      - .:/myapp
      - bundle:/bundle
    ports:
      - "3000:3000"
    depends_on:
      - db
volumes:
  mysql-db:
    driver: local
  bundle:
    driver: local

rails new

  • bin/rails active_storage:installをskipする場合には --skip-bundleをしてから別途 bundle install
$ docker-compose run web rails new . --force -d mysql --skip-bundle
$ docker-compose run --rm web bundle install

確認

$ ls -U -1
Dockerfile
Gemfile
Gemfile.lock
README.md
Rakefile
app/
bin/
config/
config.ru
db/
docker-compose.yml
lib/
log/
package.json
public/
storage/
test/
tmp/
vendor/

$ docker volume ls
DRIVER              VOLUME NAME
local               myapp_bundle
local               myapp_mysql-db

database.yml

  • hostdocker-composeで定義したMySQLのservcieに設定
  • passwordを設定
$ vim config/database.yml
default: &default
  adapter: mysql2
  encoding: utf8mb4
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: root
  password: <%= ENV['DB_PASSWORD'] || 'password' %>
  host: db

development:
  <<: *default
  database: myapp_development

test:
  <<: *default
  database: myapp_test

実行

$ docker-compose up

初回は --build オプションもあり

$ docker-compose up --build

確認

ブラウザ

http://0000:3000

コンテナ

  • Containerが myapp_web_1 の場合
$ docker exec -it myapp_web_1 /bin/bash

DB

$ docker-compose run --rm web rake db:create

-ActiveRecord::PendingMigrationErrorが発生する場合は、db:migrate で回避。(active_storage_blobs, active_storage_attachments を作成)

$ docker-compose run --rm -e RAILS_ENV=development web bin/rails db:migrate

確認

$ docker-compose up
$ mysql -h 127.0.0.1 -D myapp_development -uroot -p

git clone 後

$ docker-compose up --build
$ docker-compose run --rm web rake db:create

関連本

Ruby on Rails 5アプリケーションプログラミング

Ruby on Rails 5アプリケーションプログラミング

Docker

Docker