19
22

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Ruby on Railsの開発環境をDocker化する

Last updated at Posted at 2020-03-03

*ほぼ公式のコピペで書いたのであとでなぜそうなのかというところは追記する

※Mac OS
※すでにRubyでアプリは作成済み
 自分はVirtualBox,vagrant環境、Railsチュートリアルで6章まで作ったアプリで試しました
※DBはpostgresを使用
※ruby version 2.6.3
※rails version 5.1.6

下準備

  1. Docker for Macをインストールする
  2. docker --versionでインストールできたかを確認
  3. その後の基本的なことは公式( https://docs.docker.com/compose/rails/ )に書いてある

公式( https://docs.docker.com/compose/rails/ )の概要

Dockerfile作成

  • Rubyのプロジェクトに移動しtouch Dockerfile
    • 以下のmyappは自分が作ったアプリ名に変える
FROM ruby:2.5 →自分が使うアプリのrubyのバージョン
RUN apt-get update -qq && apt-get install -y nodejs postgresql-client
RUN mkdir /myapp
WORKDIR /myapp
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock
RUN bundle install
COPY . /myapp

# Add a script to be executed every time the container starts.
COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
EXPOSE 3000

# Start the main process.
CMD ["rails", "server", "-b", "0.0.0.0"]

entrypoint.sh 作成

  • touch entrypoint.sh
    • 以下のmyappは自分が作ったアプリ名に変える

#!/bin/bash
set -e

# Remove a potentially pre-existing server.pid for Rails.
rm -f /myapp/tmp/pids/server.pid

# Then exec the container's main process (what's set as CMD in the Dockerfile).
exec "$@"

docker-compose.yml 作成

  • touch docker-compose.yml
    • 以下のmyappは自分が作ったアプリ名に変える

version: '3'
services:
  db:
    image: postgres
    volumes:
      - ./tmp/db:/var/lib/postgresql/data
  web:
    build: .
    command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
    volumes:
      - .:/myapp
    ports:
      - "3000:3000"
    depends_on:
      - db

ビルド

docker-compose run web rails new . --force --no-deps --database=postgresql

エラーになった場合

/usr/local/lib/ruby/2.6.0/rubygems.rb:283:in `find_spec_for_exe': Could not find 'bundler' (2.0.2) required by your /sample_app/Gemfile.lock. (Gem::GemNotFoundException)
To update to the latest version installed on your system, run `bundle update --bundler`.
To install the missing version, run `gem install bundler:2.0.2`
	from /usr/local/lib/ruby/2.6.0/rubygems.rb:302:in `activate_bin_path'
	from /usr/local/bin/bundle:23:in `<main>'

DcokerfileのRUN bundle installの前に一行追加

COPY Gemfile.lock /myapp/Gemfile.lock
RUN gem install bundler →これを追加
RUN bundle install

そのあとにdocker-compose build

DBの接続設定

  • config/database.yml
    • 以下のmyappは自分が作ったアプリ名に変える

default: &default
  adapter: postgresql
  encoding: unicode
  host: db
  username: postgres
  password:
  pool: 5

development:
  <<: *default
  database: myapp_development


test:
  <<: *default
  database: myapp_test

Dockerコンテナ起動

  • docker-compose up
DBがエラーになった場合
db_1   | Error: Database is uninitialized and superuser password is not specified.
db_1   |        You must specify POSTGRES_PASSWORD to a non-empty value for the
db_1   |        superuser. For example, "-e POSTGRES_PASSWORD=password" on "docker run".
db_1   | 
db_1   |        You may also use "POSTGRES_HOST_AUTH_METHOD=trust" to allow all
db_1   |        connections without a password. This is *not* recommended.
db_1   | 
db_1   |        See PostgreSQL documentation about "trust":
db_1   |        https://www.postgresql.org/docs/current/auth-trust.html
sample_app_db_1 exited with code 1

docker-compose.ymlにパスワードを記載


services:
  db:
    image: postgres
    volumes:
      - ./tmp/db:/var/lib/postgresql/data
    environment: →ここを追加
      POSTGRES_PASSWORD: 'postgres' →ここを追加

もう一回docker-compose up

database system is ready to accept connections

これが出ればOK

DB作成

別ターミナルを開いて以下のコマンド
docker-compose run web rake db:create

エラーだった場合
Gem::LoadError: You have already activated rake 12.3.2, but your Gemfile requires rake 13.0.1. Prepending bundle exec to your command may solve this.

このコマンドでやってみる
docker-compose run web bundle exec rake db:create

エラーだった場合
PG::ConnectionBad: fe_sendauth: no password supplied

config/database.ymlにDBのパスワードを設定

username: postgres
password: postgres ←これ
pool: 5

DBをマイグレーション(すでにプロジェクトを作成済みでマイグレーションファイルがある場合)
docker-compose run web bundle exec rake db:migrate

Railsの起動を確認

http://localhost:3000/ 
へアクセス

スクリーンショット 2020-03-03 23.22.10.png

やった!!!!!!!!

一応docker ps -a で起動してるかも確認
sample_app_web
sample_app_web_1
が動いていればOK

19
22
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
19
22

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?