docker-composeでRails4の開発をする流れ

## 参考

* [docker-compose で Rails の開発環境を作る - Qiita](http://qiita.com/skyriser/items/2cf98b747ed6577cc5ee)

## 手順

* ./app : railsプロジェクトを準備しておく。

```bash:準備方法の例
mkdir app && cd app
rails new .
bundle install
```

* ./Dockerfile : rails用Dockerfile
* ./docker-compose.yml : postgresとrailsの連携

```bash:Dockerfile
FROM rails

RUN \
  unlink /etc/localtime ;\
  ln -s /usr/share/zoneinfo/Japan /etc/localtime ;\
  sed -i -e 's@http://archive@http://jp.archive@' /etc/apt/sources.list ;\
  apt-get update -qq ;\
  apt-get install -y -qq locales ;\
  locale-gen ja_JP.UTF-8 ;\
  apt-get clean
#----------------------------------------------------
WORKDIR /tmp
COPY ./app/Gemfile Gemfile
COPY ./app/Gemfile.lock Gemfile.lock
RUN bundle install

ADD ./app /app
WORKDIR /app

VOLUME /app
EXPOSE 3000
```

* `COPY` と `ADD` が重要。

```docker-compose.yml
db:
  image: postgres
  expose:
    - "5432"
web:
  build: .
  command: bundle exec rails s -p 3000 -b '0.0.0.0'
  volumes:
    - ./app:/app
  ports:
    - "3000:3000"
  links:
    - db
```

----

```bash:起動
docker-compose build
docker-compose up
```


## 起動時にエラーがでる場合

> web_1 | => Booting WEBrick
> web_1 | => Rails 4.2.4 application starting in development on http://0.0.0.0:3000
> web_1 | => Run `rails server -h` for more startup options
> web_1 | => Ctrl-C to shutdown server
> web_1 | A server is already running. Check /app/tmp/pids/server.pid.
> web_1 | Exiting
> rails4_web_1 exited with code 1

* プロセスが停止しているのに app/tmp/pids/server.pid が残っていることが原因。

```bash:
rm -f app/tmp/pids/server.pid
```

## index.htmlを編集してみる

![r1.png](https://qiita-image-store.s3.amazonaws.com/0/25728/b44401bb-c2eb-a1e8-055c-72b23b25709b.png)

```bash:app/public/index.html
hello
```

![r2.png](https://qiita-image-store.s3.amazonaws.com/0/25728/9314ca5a-4c2d-a087-9f63-8106cb934b3c.png)

## ルーティングの追加

```bash:現在ルーティング定義がないことを確認
$ docker-compose run web rake routes
You don't have any routes defined!

Please add some routes in config/routes.rb.

For more information about routes, see the Rails guide: http://guides.rubyonrails.org/routing.html.
```

```diff:config/routes.rb
 Rails.application.routes.draw do
+    resources :products do
+      member do
+        get 'short'
+        post 'toggle'
+      end
+ 
+      collection do
+        get 'sold'
+      end
+    end
 end
```

```bash:ルーティングが追加されたことを確認
$ docker-compose run web rake routes
        Prefix Verb   URI Pattern                    Controller#Action
 short_product GET    /products/:id/short(.:format)  products#short
toggle_product POST   /products/:id/toggle(.:format) products#toggle
 sold_products GET    /products/sold(.:format)       products#sold
      products GET    /products(.:format)            products#index
               POST   /products(.:format)            products#create
   new_product GET    /products/new(.:format)        products#new
  edit_product GET    /products/:id/edit(.:format)   products#edit
       product GET    /products/:id(.:format)        products#show
               PATCH  /products/:id(.:format)        products#update
               PUT    /products/:id(.:format)        products#update
               DELETE /products/:id(.:format)        products#destroy
```

----

## [小学生でもわかるRuby on Rails入門のメモ - Qiita](http://qiita.com/tukiyo3/items/6fb9294bfb85e8a49861)を実践

```bash:
docker-compose run web \
rails g controller users index show
```

```bash:コンテナ再作成
docker-compose build
docker-compose up
```

* http://localhost:3000/users/show にアクセスし画面が表示されることを確認。

![r3.png](https://qiita-image-store.s3.amazonaws.com/0/25728/51e6c075-7259-782f-5014-cf6e5a10a545.png)

### sqlite3からpostgresqlに変更

* 参考:[docker-compose - Docker Compose - クイックスタート・ガイド：Compose と Rails - Qiita](http://qiita.com/zembutsu/items/5cc40fb4643dbc8448b6)

```diff:Gemfile
+ gem 'pg'
```

```yaml:config/database.yml
default: &default
  adapter: postgresql
  encoding: unicode
  pool: 5
  username: postgres
  password:
  host: db

development:
  <<: *default
  database: sample_app_development

test:
  <<: *default
  database: sample_app_test

production:
  <<: *default
  database: sample_app_production
```

```bash:コンテナ再作成
docker-compose build
docker-compose run web rake db:create
docker-compose up
```

* DBの作成のため初回のみ`docker-compose run web rake db:create`が必要。

### modelの作成

```bash:user.{name,username,location,about}カラム作成
docker-compose run web \
rails g model user name:string username:string location:string about:text
```

```bash:コンテナ再作成
docker-compose build
docker-compose run web rake db:migrate
docker-compose up
```

### 初期データの投入

```rb:db/seeds.rb
@user = User.new
@user.name = 'Ryo Suzuki'
@user.username = 'ryooopan'
@user.location = 'Kanagawa, Japan'
@user.about = 'Hello, I am Ryo. I am from database!'
@user.save

@user = User.new
@user.name = 'Shohei Aoki'
@user.username = 'moyahima'
@user.location = 'Tottori, Japan'
@user.about = 'Nice to meet you. I am from database!'
@user.save
```

```bash:初期データの投入
docker-compose run web \
rake db:seed
```

```diff:config/routes.rb
Rails.application.routes.draw do
  get "users/index"
  get 'users/show'
+ get "users/show/:username" => "users#show"
```

```bash:
cd app
mkdir -p app/views/users
mkdir app/controllers
```

```html+erb:app/views/users/show.html.erb
<h1><%= @user[:name] %></h1>
<p><%= @user[:username] %></p>
<ul>
  <li>Location : <%= @user[:location] %></li>
  <li>About    : <%= @user[:about] %></li>
</ul>
```

```rb:app/controllers/users_controller.rb
class UsersController < ApplicationController
  def index
  end

  def show
    @user = User.find_by(:username => params[:username])
  end
end
```

* http://localhost:3000/users/show/ryooopan にアクセスし以下表示を確認

![r4.png](https://qiita-image-store.s3.amazonaws.com/0/25728/f5324dd6-49dc-5253-239c-0c8f60966870.png)