#author("2017-02-21T19:21:06+09:00","default:wikiwriter","wikiwriter") &tag(Docker/実践); *目次 [#nfebf151] #contents *関連ページ [#u1a0a84a] *参考情報 [#ya9a5530] *RailsをDockerで動かす [#ndef635d] -公式ガイド[[Quickstart: Compose and Rails - Docker:https://docs.docker.com/compose/rails/]]が存在。 **プロジェクトの作成 [#s6b01b5e] -Dockerfileを作成する(webサーバー用) #pre{{ FROM ruby:2.3.3 RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs RUN mkdir /myapp WORKDIR /myapp ADD Gemfile /myapp/Gemfile ADD Gemfile.lock /myapp/Gemfile.lock RUN bundle install ADD . /myapp }} -同じフォルダにGemfileを作成 #pre{{ source 'https://rubygems.org' gem 'rails', '5.0.0.1' }} -同じフォルダにGemfile.lockを作成 #pre{{ touch Gemfile.lock }} -docker-compose.ymlを作成。webコンテナはdepends_onでdbコンテナを参照しているので、"db"という名前を使って参照できる(linksと同じ)。 #pre{{ version: '2' services: db: image: postgres web: build: . command: bundle exec rails s -p 3000 -b '0.0.0.0' volumes: - .:/myapp ports: - "3000:3000" depends_on: - db }} **プロジェクトのビルド [#mcdaa46b] -以下のコマンドでRailsスケルトンアプリを実行。 #pre{{ docker-compose run web rails new . --force --database=postgresql --skip-bundle }} -composeによってwebイメージが作られる。rails newがコンテナ内部で実行されローカルにファイルが書き出される(volumesの効能)。 -Gemfileを変更したときは「docker-compose build」を実行。 **データベースとの接続 [#k500c382] -config/database.ymlの作成 #pre{{ development: &default adapter: postgresql encoding: unicode database: myapp_development pool: 5 username: postgres password: host: db test: <<: *default database: myapp_test }} -起動 docker-compose up -他のターミナルを開きdb生成 docker compose run web rake db:create **トラブルシューティング [#y996932e] ***server is already running [#t10e0a9f] -[[RailsアプリケーションをDockerを使って開発する、ほとんどの人が通る(であろう)道 - Qiita:http://qiita.com/Shunsuke-Komuta/items/89c31d647bf42bf2300c]] -[[Rails server is still running in a new opened docker container - Stack Overflow:http://stackoverflow.com/questions/35022428/rails-server-is-still-running-in-a-new-opened-docker-container]] -server.pidファイルが残っている場合、それを削除する。 -上のサンプルの場合tmp/pids/server.pidを確認。 -次の方法もあり? command: /bin/sh -c "rm -f tmp/pids/server.pid && rails server puma" **サーバーは動いているけどページが表示されない [#f20a3d13] -[[DockerでRailsの開発環境つくったらweb-console動かない - Qiita:http://qiita.com/kazukgw@github/items/5e08ce893b709e16e1e1]] -config/environments/development.rbに以下を追加 config.web_console.whitelisted_ips = '0.0.0.0/0'