LoginSignup
1

More than 3 years have passed since last update.

Rails(6.02) + PostgreSQL(12.2) の環境をdocker-composeで作成する(webpacker対応版)

Last updated at Posted at 2020-03-26

はじめに

Rails+PostgreSQLの環境をdocker-composeで作成する - Qiitaで紹介されている方法はRails5での動作を前提としているようでした。
Rails6のwebpackerまわりの記述を追加し、アップデートしたのがこの記事になります。

Version

  • Ruby 2.7.0
  • Rails 6.0.2 (Dockerfileでバージョン固定します)
  • PostgreSQL 12.2

ファイル構成

元記事におなじです。

./
|- app/  //共有フォルダ
|- docker-compose.yml
|- web/
   |- Dockerfile

Dockerfileの作成

Dockerfile
FROM ruby:2.7.0

RUN 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 -qq \
  && apt-get install -y build-essential libpq-dev postgresql-client \
  && apt-get install -y nodejs yarn

RUN gem install rails -v "6.0.2"
RUN mkdir /app
WORKDIR /app

Dockerfileの変更点

  • Rubyのバージョンアップ FROM ruby:2.7.0
  • Railsのバージョン固定 RUN gem install rails -v "6.0.2"
    • 元記事でもおなじようにバージョン固定(-v "5.2.3"とか)すればそのまま使えるはず(未調査)
  • webpacker用のライブラリ(yarnとnodejs)の追加
yarnとnodejsの追加部分
RUN 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 -qq \
  && apt-get install -y nodejs yarn

docker-compose.ymlの作成

docker-compose.yml
version: "3"

services:
  web:
    build: web
    ports:
      - "3000:3000"
    environment:
      - "DATABASE_HOST=db"
      - "DATABASE_PORT=5432"
      - "DATABASE_USER=postgres"
      - "DATABASE_PASSWORD=admin"
    links:
      - db
    volumes:
      - "./app:/app" #共有フォルダの設定
    stdin_open: true

  db:
    image: postgres:12.2
    ports:
      - "5432:5432"
    environment:
      - "POSTGRES_USER=postgres"
      - "POSTGRES_PASSWORD=admin"

docker-compose.ymlの変更点

  • postgreSQLのバージョンアップ image: postgres:12.2

コンテナを起動する

docker-compose build
docker-compose up -d

コンテナにログインし、Railsアプリを作成

docker-compose exec web bash
rails new <アプリ名> -d postgresql -BT

Gemfile

Gemfile
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '2.7.0'

gem 'rails', '~> 6.0.2'
gem 'pg', '>= 0.18', '< 2.0'
gem 'puma', '~> 4.1'
gem 'sass-rails', '>= 6'
gem 'webpacker', '~> 4.0'
gem 'turbolinks', '~> 5'
gem 'bootsnap', '>= 1.4.2', require: false

group :development, :test do
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
end

group :development do
  gem 'web-console', '>= 3.3.0'
  gem 'listen', '>= 3.0.5', '< 3.2'
  gem 'spring'
  gem 'spring-watcher-listen', '~> 2.0.0'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

config/database.yml

アプリ名は「yay」にしています。

config/database.yml
default: &default
  adapter: postgresql
  encoding: unicode
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  host: <%= ENV.fetch('DATABASE_HOST') { 'localhost' } %>
  port: <%= ENV.fetch('DATABASE_PORT') { 5432 } %>
  username: <%= ENV.fetch('DATABASE_USER') { 'root' } %>
  password: <%= ENV.fetch('DATABASE_PASSWORD') { 'password' } %>

development:
  <<: *default
  database: yay_development

test:
  <<: *default
  database: yay_test

production:
  <<: *default
  database: yay_production
  username: yay
  password: <%= ENV['YAY_DATABASE_PASSWORD'] %>

Railsアプリを起動する

作成したアプリのディレクトリに移動して、もろもろのセットアップをおこなえばアプリが起動できるはずです :smile:

cd <アプリ名>
bundle install
rails webpacker:install
rails db:create
rails db:migrate
rails s -b 0.0.0.0 -d 

サンプルレポジトリ

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
1