Rails on Docker on OSX環境構築

![](http://applech2.com/wp-content/uploads/2016/07/Docker-logo-icon.jpg)![](http://findicons.com/files/icons/1607/ruby_on_rails/128/ror_logo.png)

```bash
mkdir container_01
docker pull ruby:2.4.1-alpine
touch Dockerfile docker-compose.yml Gemfile Gemfile.lock
```

`docker-compose up -d --build`で使用される


```:Dockerfile
##### Base Image #####
FROM ruby:2.4.1-alpine

ENV LANG en_US.UTF-8

##### イメージ上でコマンドを実行 #####
RUN apk update && \
    apk upgrade && \
    apk add --update\
    bash \
    build-base \
    curl-dev \
    git \
    libxml2-dev \
    libxslt-dev \
    linux-headers \
    mysql-dev \
    nodejs \
    openssh \
    ruby-dev \
    ruby-json \
    tzdata \
    yaml \
    yaml-dev \
    zlib-dev

RUN gem install bundler

##### WORKDIRを指定してコマンド実行 #####
WORKDIR /tmp
COPY Gemfile Gemfile
COPY Gemfile.lock Gemfile.lock
RUN bundle install

ENV APP_HOME /myapp
RUN mkdir -p $APP_HOME
WORKDIR $APP_HOME
COPY . $APP_HOME
```

```:Gemfile
source 'https://rubygems.org'
gem 'rails'
```

```:docker-compose.yml
version: '2'
services:
  web:
    build: .
    command: bundle exec rails s -p 3000 -b '0.0.0.0'
    volumes:
      - .:/myapp
    ports:
      - "3000:3000"
    depends_on:
      - db
    container_name: web

  db:
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD: root_password
    volumes_from:
      - datastore
    container_name: db

  datastore:
    image: busybox
    volumes:
      - ./datastore_container:/var/lib/mysql
    container_name: datastore
```

```bash
docker-compose run web rails new . --force --database=mysql --skip-bundle
```

JavaScript runtime 用に therubyracer の gem インストールを有効化
[therubyracer](http://stackoverflow.com/questions/23536893/therubyracer-gemextbuilderror-error-failed-to-build-gem-native-extension)

* gem 'therubyracer', '0.11.3'
* gem 'libv8', '3.11.8.13'

```:Gemfile
source 'https://rubygems.org'

git_source(:github) do |repo_name|
  repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/")
  "https://github.com/#{repo_name}.git"
end

gem 'rails', '~> 5.0.2'
gem 'mysql2', '>= 0.3.18', '< 0.5'
gem 'puma', '~> 3.0'
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.2'
gem 'jquery-rails'
gem 'turbolinks', '~> 5'
gem 'jbuilder', '~> 2.5'
gem 'therubyracer', '0.11.3'
gem 'libv8', '3.11.8.13'
group :development, :test do
  gem 'byebug', platform: :mri
end
group :development do
  gem 'web-console', '>= 3.3.0'
  gem 'listen', '~> 3.0.5'
  gem 'spring'
  gem 'spring-watcher-listen', '~> 2.0.0'
end
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
```

```:config/database.yml
default: &default
  adapter: mysql2
  encoding: utf8
  pool: 5
  username: root
  password: root_password
  host: db

development:
  <<: *default
  database: container_development

test:
  <<: *default
  database: container_test

production:
  <<: *default
  database: myapp_production
  username: myapp
  password: <%= ENV['MYAPP_DATABASE_PASSWORD'] %>
```

```bash
docker-compose up -d --build
docker-compose run web rails db:create
docker-compose run web rails generate scaffold user name:string
docker-compose run web rails db:migrate
```

```bash
gem install libv8 -v '3.11.8.17' -- --with-system-v8
```