LoginSignup
5
3

More than 3 years have passed since last update.

Docker + Rails6 + PostgreSQL 環境構築

Last updated at Posted at 2020-07-15

はじめに

今までRails5 + MySQL での開発しか行ってこなかった自分でしたが、初めてRails6 + PostgreSQL での環境構築を行ったため備忘録も兼ねて記述していきたいと思います。

必要なファイル

作業フォルダを作成します。
今回は sample_app とします。

$ mkdir sample_app

そして必要なファイルを以下の構成で作成します。

$ touch ○○(ファイル名)

- sample_app
    - Dockerfile
    - docker-compose.yml
    - Gemfile
    - Gemfile.lock
    - entrypoint.sh

ではそれぞれを編集していきます。

Dockerfile

Rails6 からWebpackerがデフォルトで導入されたためインストールする必要があります。
そのため、DockerfileにはWebpackerをインストールするために必要なyarnNode.jsをインストールするように記述します。

FROM ruby:2.6.5

# 必要なライブラリインストール
RUN apt-get update -qq && apt-get install -y nodejs postgresql-client

# yarnパッケージ管理ツールをインストール
RUN apt-get update && apt-get install -y curl apt-transport-https wget && \
  curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - && \
  echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list && \
  apt-get update && apt-get install -y yarn

# Node.jsをインストール
RUN curl -sL https://deb.nodesource.com/setup_7.x | bash - && \
  apt-get install nodejs

RUN mkdir /sample_app
WORKDIR /sample_app
COPY Gemfile /sample_app/Gemfile
COPY Gemfile.lock /sample_app/Gemfile.lock
RUN bundle install
COPY . /sample_app

COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
EXPOSE 3000

CMD ["rails", "server", "-b", "0.0.0.0"]

docker-compose.yml

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

Gemfile

source 'https://rubygems.org'
gem 'rails', '~> 6'

Gemfile.lock

ファイルを作成だけして、編集は何もしません。

entrypoint.sh

entrypoint.sh
#!/bin/bash
set -e

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

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

Railsアプリの作成

作成したDockerfile元にイメージがbuildし、生成されたコンテナ内でrails newを実行します

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

bundle install

rails newコマンドによりGemfile内の記述が変更されているためbundle installをする必要があります。
イメージをbuildすることでbundle installも実行されるため以下のコマンドを実行します。

$ docker-compose build

database.yml

rails newで作成されたdatabase.ymlを以下のように編集します。

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

development:
  <<: *default
  database: sample_app_development


test:
  <<: *default
  database: sample_app_test

webpackerのインストール

$ docker-compose run web bundle exec rails webpacker:install

コンテナを起動

$ docker-compose up

DB作成

$ docker-compose run web rails db:create

おわり

これでhttp://localhost:3000/にアクセスするとサーバーが立ち上がっていることを確認できるかと思います。
最後まで見ていただき、ありがとうございました。

参考記事
DockerでRuby on Railsの環境構築を行うためのステップ【Rails 6対応】
Rails6 開発時につまづきそうな webpacker, yarn 関係のエラーと解決方法
PostgreSQL(Docker)にRails(Docker)が接続できなくなったから調べてみた。(could not translate host name "db" to address: Name or service not known)

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