LoginSignup
15
17

More than 5 years have passed since last update.

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

Last updated at Posted at 2015-09-07

参考

手順

  • ./app : railsプロジェクトを準備しておく。
準備方法の例
mkdir app && cd app
rails new .
bundle install
  • ./Dockerfile : rails用Dockerfile
  • ./docker-compose.yml : postgresとrailsの連携
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
  • COPYADD が重要。
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

起動
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 が残っていることが原因。
rm -f app/tmp/pids/server.pid

index.htmlを編集してみる

r1.png

app/public/index.html
hello

r2.png

ルーティングの追加

現在ルーティング定義がないことを確認
$ 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.
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
ルーティングが追加されたことを確認
$ 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を実践

docker-compose run web \
rails g controller users index show
コンテナ再作成
docker-compose build
docker-compose up

r3.png

sqlite3からpostgresqlに変更

Gemfile
+ gem 'pg'
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
コンテナ再作成
docker-compose build
docker-compose run web rake db:create
docker-compose up
  • DBの作成のため初回のみdocker-compose run web rake db:createが必要。

modelの作成

user.{name,username,location,about}カラム作成
docker-compose run web \
rails g model user name:string username:string location:string about:text
コンテナ再作成
docker-compose build
docker-compose run web rake db:migrate
docker-compose up

初期データの投入

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
初期データの投入
docker-compose run web \
rake db:seed
config/routes.rb
Rails.application.routes.draw do
  get "users/index"
  get 'users/show'
+ get "users/show/:username" => "users#show"
cd app
mkdir -p app/views/users
mkdir app/controllers
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>
app/controllers/users_controller.rb
class UsersController < ApplicationController
  def index
  end

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

r4.png

15
17
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
17