LoginSignup
6
3

More than 3 years have passed since last update.

railsの環境構築 その1

Last updated at Posted at 2020-02-11

はじめに

これからdockerで開発環境の構築からAWSデブロイまで数回に分けてまとめてみます

私が使った有料教材

Dockerについて

Dockerについては下記のサイトを見てください。
* 入門 Docker

Dockerでrailsの環境構築

参考

作業手順

ディレクトリを作成しそこで作業

mkdir ror_app

Dockerfileを作成Dockerfile内に下記を記述

touch Dockerfile
FROM ruby:2.5
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"]

コマンド

FROM: ベースとなるDocker Image を指定します。
COPY: Docker内へホストのファイル/ディレクトリをコピーします。
  COPY は基本的に2つの引数を設定します。1つ目はホスト側のディレクトリ、2つ目はDocker側のディレクトリです。
RUN: Docker内でコマンドを実行します。
CMD: Docker起動時にデフォルトで実行されるコマンドを定義します。
WORKDIR: Dockerfileでコマンドを実行する際に基準となるディレクトリを設定します。
EXPOSE: コンテナ起動時に公開することを想定されているポートを記述します
ENTRYPOINT: 指定されたコマンドを実行します。

GemfileとGemfile.lockの作成

  • rails newするときに必要
touch Gemfile && touch Gemfile.lock
source 'https://rubygems.org'
gem 'rails', '~>5'

entrypoint.shを作成

  • 特定のserver.pidファイルが存在するときにサーバーが再起動しないようにするRails固有の問題を修正するエントリポイントスクリプトを提供します。このスクリプトは、コンテナが開始されるたびに実行されます。
  • touch entrypoint.sh
entrypoint.sh
#!/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
docker-compose.yml
version: '3'
services:
  db:
    image: postgres
    ports:
      - '5432:5432'
    volumes:
      - postgresql-data:/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
volumes:
  postgresql-data:
    driver: local

docker上でrailsのファイルを作成

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

# rails new . とすることで、カレントディレクトリ配下にrailsファイルが作成される  
--forceで事前に作成したファイルを上書き、今回はGemfile等

gem 'pg'のバージョン変更

gem 'pg', '~> 0.20.0'

イメージをbuild

docker-compose build

すると

ror_app_web:latestになります。

databese.ymlの書き換え

default: &default
  adapter: postgresql
  encoding: unicode
  host: db
  username: postgres
  password:
  pool: 5
# host,username,passwordを追加しました。
hostをdbにすることでdocker-compose.ymlのdbを指定

本番環境や開発環境でdatabase関連のエラーが良く出てきます。
特に本番環境をAWSにするとproduciton:配下を書き換えないといけません。

データベースの作成

docker-compose run web rake db:create

サービスの起動

docker-compose up
# 終了はCtrl+C

ブラウザでlocalhost:3000にアクセスするとrailsの初期画面が出現したと思います。

docker-composeコマンドまとめ

ローカル環境 docker環境
rails s docker-compose up
rails db:migrate docker-compose run web rails db:migrate
bundle install docker-compose run web bundle install
rails g model モデル名 docker-compose run web rails g model モデル名

git push

railsの初期画面が出てきたらgit pushしておきましょう。

お疲れさまでした。

6
3
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
6
3