LoginSignup
0
0

More than 1 year has passed since last update.

Ruby on Rails7の開発環境構築(PostgreSQL版)

Last updated at Posted at 2023-01-31

概要

※当記事は所属している株式会社コンピュータテクノブレインにて投稿した内容を転載したものです。

Ruby on Rails 7Dockerを使用した開発環境の構築についての手順を紹介します。

あまりPostgreSQLを使用した例がなかったためPostgreSQLを使用した環境を構築してみようと思います。

構成

2022年5月18日時点での環境を示します。

  • macOS Monterey 12.3.1
  • Docker 20.10.14
  • Docker Compose v2.5.1
  • Ruby 3.1.2
  • Ruby on Rails 7.0.3
  • PostgreSQL 14.3

ディレクトリ構造

プロジェクトルート
├── docker-compose.yml
├── docker
│   ├── db
│   ├── ruby
│       └── Dockerfile
└── web
    ├── Gemfile
    └── Gemfile.lock

新規プロジェクトの作成

ソースコードの編集

  • docker-compose.yml
version: '3'
services:
  db:
    image: postgres:14.3-alpine
    container_name: postgres
    ports:
      - "5432:5432"
    volumes:
      - ./docker/db/data:/var/lib/postgresql/data
    environment:
      - TZ=Asia/Tokyo
      - PGTZ=Asia/Tokyo
      - POSTGRES_PASSWORD=password
  web:
    build:
      context:.
      dockerfile: /docker/ruby/Dockerfile
    command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
    volumes:
      - ./web:/app
    ports:
      - "3000:3000"
    depends_on:
      - db
  • docker/ruby/Dockerfile
FROM ruby:3.1.2-alpine

ENV ROOT="/app"
ENV LANG=C.UTF-8
ENV TZ=Asia/Tokyo

WORKDIR ${ROOT}

COPY ./web/Gemfile ./web/Gemfile.lock ${ROOT}

RUN apk update && \
    apk add \
    alpine-sdk \
    build-base \
    sqlite-dev \
    postgresql-dev \
    tzdata \
    git \
    gcompat

RUN gem install bundler
RUN bundle install

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

CMD["rails", "server", "-b", "0.0.0.0"]
  • web/Gemfile
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby "3.1.2"

# Bundle edge Rails instead: gem "rails", github: "rails/rails", branch: "main"
gem "rails", "~> 7.0.3"
  • web/Gemfile.lock
空ファイル
  • docker/ruby/entrypoint.sh
#!/bin/ash
set -e

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

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

Ruby on Railsプロジェクトを作成する

docker compose run --rm web bundle exec rails _7.0.3_ new . -d postgresql -f

コンテナをビルドする

docker compose build

アプリケーションを起動する

データベース情報を設定する

  • web/config/database.yml
 default: &default
   adapter: postgresql
   encoding: unicode
   # For details on connection pooling, see Rails configuration guide
   # https://guides.rubyonrails.org/configuring.html#database-pooling
   pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
+  username: postgres
+  password: password
+  host: db

データベースを作成する

docker compose run --rm web rails db:create

コンテナを起動する

docker compose up -d

http://localhost:3000/にアクセスする

7caaf96d1b1f77afa2db65ed9a75c16a-1024x509.png

弊社にお仕事を依頼したいお客様がいらっしゃいましたら、以下のフォームもしくはメールにてお気軽にお問い合わせ下さい。

お問合せ

システム開発部 森岡(morioka_tatsuaki@computer-tb.co.jp

0
0
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
0
0