hiko1129’s blog

開発に関することを記録するためのブログ

ruby 2.6 + alpine 3.8 + Rails 5.2 + Docker + docker-compose + mysql 5.6 開発環境構築

Dockerfileの準備

下記Dockerfileを準備

FROM ruby:2.6-alpine3.8

ENV APP_ROOT /usr/src/app

WORKDIR $APP_ROOT

RUN apk add --no-cache alpine-sdk \
    nodejs-current \
    nodejs-npm \
    yarn \
    mysql-client \
    mysql-dev \
    python2 \
    tzdata

ruby2.6、alpine3.8で構築。webpakcerが入れられるようにnodeとyarn入り。 mysqlを使っていくのでmysql関連のパッケージも追加済み。 python2はnodeが依存しているため追加している。

docker-compose.ymlの準備

下記docker-compose.ymlを準備

version: '2'
services:
  mysql-development:
    image: mysql:5.6
    environment:
      MYSQL_ALLOW_EMPTY_PASSWORD: "yes"
      MYSQL_USER: app
      MYSQL_PASSWORD: app
      MYSQL_DATABASE: app_development
      TZ: Asia/Tokyo
    expose:
      - '3306'
    volumes:
      - mysql-development-data:/var/lib/mysql
      - ./mysql5.6:/etc/mysql/conf.d

  mysql-test:
    image: mysql:5.6
    environment:
      MYSQL_ALLOW_EMPTY_PASSWORD: "yes"
      MYSQL_USER: app
      MYSQL_PASSWORD: app
      MYSQL_DATABASE: app_test
      TZ: Asia/Tokyo
    expose:
      - '3306'
    volumes:
      - mysql-test-data:/var/lib/mysql
      - ./mysql5.6:/etc/mysql/conf.d


  app:
    tty: true
    stdin_open: true
    build:
      context: .
      dockerfile: Dockerfile
    environment:
      BUNDLE_JOBS: 4
      BUNDLE_PATH: /usr/src/app/vendor/bundle
    command: bin/server
    ports:
      - '13000:3000'
    volumes:
      - .:/usr/src/app
      - bundle-data:/usr/src/app/vendor/bundle
      - node_modules-data:/usr/src/app/node_modules
      - log-data:/usr/src/app/log
    links:
      - mysql-test
      - mysql-development
      
volumes:
  mysql-development-data:
    driver: local
  mysql-test-data:
    driver: local
  bundle-data:
    driver: local
  node_modules-data:
    driver: local
  log-data:
    driver: local

コンテナの3000番ポートをホストの13000番ポートに割り当てているため、13000番ポートにアクセスすることでブラウザで表示させることができる。

Gemfileの準備

下記Gemfileを準備

source 'http://rubygems.org'
gem 'rails', '5.2.0'

Gemfile.lockの準備

空のGemfile.lockを準備

Railsプロジェクトの作成

上記ファイルをすべてプロジェクト用のディレクトリ直下に置いて下記コマンドを実行

docker-compose run --rm app bundle install
docker-compose run --rm app bundle exec rails new . --force --database=mysql

bin/serverの準備

docker-composeのcommandに直接記述すると長くなるので、bin/serverに下記内容を記述し、パーミッションを変更して、commandからbin/serverを呼び出すようにしておく。

#!/usr/bin/env ruby
require 'pathname'
require 'fileutils'
include FileUtils

# path to your application root.
APP_ROOT = Pathname.new File.expand_path('..', __dir__)

def system!(*args)
  system(*args) || abort("\n== Command #{args} failed ==")
end

chdir APP_ROOT do
  puts "\n== Removing pid =="
  system! 'rm -f /usr/src/app/tmp/pids/server.pid'

  puts "\n== Installing Gems =="
  system! 'bin/bundle install'

  puts "\n== Installing JavaScript dependencies =="
  system! 'bin/yarn install'

  puts "\n== Starting server =="
  system! 'bundle exec rails s -b 0.0.0.0 -p 3000'
end

DB周りの設定追記

config/database.ymlを下記のように変更

# MySQL. Versions 5.1.10 and up are supported.
#
# Install the MySQL driver
#   gem install mysql2
#
# Ensure the MySQL gem is defined in your Gemfile
#   gem 'mysql2'
#
# And be sure to use new-style password hashing:
#   https://dev.mysql.com/doc/refman/5.7/en/password-hashing.html
#
default: &default
  adapter: mysql2
  encoding: utf8mb4
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  host: localhost
  username: root
  password:
  charset: utf8mb4
  collation: utf8mb4_bin

development:
  <<: *default
  database: app_development
  username: app
  password: app
  host: mysql-development

# 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: app_test
  username: app
  password: app
  host: mysql-test

# As with config/secrets.yml, you never want to store sensitive information,
# like your database password, in your source code. If your source code is
# ever seen by anyone, they now have access to your database.
#
# Instead, provide the password as a unix environment variable when you boot
# the app. Read http://guides.rubyonrails.org/configuring.html#configuring-a-database
# for a full rundown on how to provide these environment variables in a
# production deployment.
#
# On Heroku and other platform providers, you may have a full connection URL
# available as an environment variable. For example:
#
#   DATABASE_URL="mysql2://myuser:mypass@localhost/somedatabase"
#
# You can use this database configuration with:
#
#   production:
#     url: <%= ENV['DATABASE_URL'] %>
#
production:
  <<: *default
  database: app_production
  username: app
  password: <%= ENV['APP_DATABASE_PASSWORD'] %>

mysql5.6/my.cnfに下記を追加

[client]
default-character-set=utf8mb4

[server]
character-set-server = utf8mb4

[mysqld]
collation-server=utf8mb4_bin
character-set-server=utf8mb4
innodb-file-format=barracuda
innodb_file_format_max=barracuda
innodb-file-per-table=1
innodb_large_prefix=1

動作確認

docker-compose up

上記コマンドを実行して、下記のような画面が表示されればとりあえず問題ないはず。

Yay! You&#x27;re on Rails
You're on Rails

下記リポジトリに生成済みのものを置いておく。 github.com