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

AWS VPC 周辺振り返り

VPC周辺の構築について記す。

VPC作成→パブリックサブネット作成→プライベートサブネット作成→インターネットゲートウェイ作成→Elastic IP割当(NATゲートウェイ用)→NATゲートウェイの作成→パブリックサブネット用のルートテーブルの作成→プライベートサブネット用のルートテーブルの作成→セキュリティグループの作成(bastion用)→bastion用起動設定の作成→bastion用Auto Scalingグループの作成」の順番で記す。

VPCの作成

VPCダッシュボードに移動し、VPCを作成する。

f:id:hiko1129:20190102152403p:plain
VPCの作成

f:id:hiko1129:20190102152547p:plain
VPC一覧

パブリックサブネットの作成。

f:id:hiko1129:20190102152718p:plainf:id:hiko1129:20190102152723p:plain
パブリックサブネットの作成

プライベートサブネットの作成。

f:id:hiko1129:20190102152835p:plainf:id:hiko1129:20190102152840p:plain
プライベートサブネットの作成

f:id:hiko1129:20190102152915p:plain
サブネット一覧

インターネットゲートウェイの作成。

f:id:hiko1129:20190102153017p:plain
インターネットゲートウェイの作成。

f:id:hiko1129:20190102153106p:plain
インターネットゲートウェイ一覧

(NATゲートウェイ用)Elastic IPの作成。

f:id:hiko1129:20190102153203p:plain
ElasticIPの作成

NATゲートウェイの作成。

f:id:hiko1129:20190102153243p:plain
NATゲートウェイの作成。

f:id:hiko1129:20190102153344p:plain
NATゲートウェイ一覧

パブリックサブネット用ルートテーブルの作成。

f:id:hiko1129:20190102180256p:plain
インターネットゲートウェイへのルーティングの追加

f:id:hiko1129:20190102180443p:plain
パブリックサブネットの関連付け

プライベートサブネット用のルートテーブルの作成。

f:id:hiko1129:20190102180546p:plain
NATゲートウェイへのルーティングの追加

f:id:hiko1129:20190102180655p:plain
プライベートサブネットの関連付け

セキュリティグループの作成。

踏み台サーバーは、自宅や自社等の固定IPが望ましいが、今回はどこからでも許容するようにしておく。

f:id:hiko1129:20190102192653p:plain
セキュリティグループの作成

f:id:hiko1129:20190102192721p:plain
セキュリティグループ一覧

起動設定の作成。

f:id:hiko1129:20190102192940p:plain
起動設定の作成(AMI)

f:id:hiko1129:20190102193006p:plain
起動設定の作成(インスタンスタイプ)

f:id:hiko1129:20190102193056p:plain
起動設定の作成(詳細設定)

f:id:hiko1129:20190102193140p:plain
起動設定の作成(ストレージの追加)

f:id:hiko1129:20190102193347p:plain
起動設定の作成(セキュリティグループの設定)

f:id:hiko1129:20190102193428p:plain
起動設定の作成(キーペア)

Auto Scaling グループの作成。

f:id:hiko1129:20190102193537p:plain
Auto Scaling グループの作成

f:id:hiko1129:20190102193617p:plain
Auto Scaling グループの作成(タグの設定)

立ち上がったEC2にSSHでつないで疎通確認。