構成
バックエンド
- Ruby 3.3
- Rails 8.0.0
※Webサーバー(Nginxなど)は今回実装していません。
データベース
- PostgreSQL 17.2
1, 最初に用意するファイル
root/
├── docker-compose.yml
├── Dockerfile.dev
├── entrypoint.sh
├── Gemfile.lock
└── Gemfiledocker-compose.ymlservices:
db:
image: postgres:17.2
environment:
POSTGRES_DB: rails_app_development
POSTGRES_USER: user
POSTGRES_PASSWORD: password
ports:
- "5432:5432"
volumes:
- postgres_data:/var/lib/postgresql/data
web:
build:
context: .
dockerfile: Dockerfile.dev
command: bash -c "bundle exec rails s -b '0.0.0.0'"
volumes:
- .:/rails-app
ports:
- 3000:3000
depends_on:
- db
environment:
- DB_HOST=db
- DB_USER=user
- DB_PASSWORD=password
- DB_NAME=rails_app_development
working_dir: /rails-app
tty: true
stdin_open: true
volumes:
postgres_data:Dockerfile.dev# ベースにするイメージを指定
FROM ruby:3.3.6
# 必要なパッケージをインストール
RUN apt-get update -qq && apt-get install -y \
build-essential \
libpq-dev \
nodejs \
vim \
&& rm -rf /var/lib/apt/lists/* \
&& apt-get clean
# 作業用ディレクトリを設定して、ディレクトリ作成と設定を同時に行う
WORKDIR /rails-app
# ローカルの Gemfile と Gemfile.lock をコンテナ内にコピー
COPY Gemfile Gemfile.lock ./
# bundle install を実行
RUN bundle install
# ローカルのrails-app配下のファイルをコンテナ内にコピー
COPY . .
# ENTRYPOINT を設定し、コンテナ起動時に常に実行されるスクリプトを指定
COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
# CMD を設定し、デフォルトのサーバ起動コマンドを指定
CMD ["rails", "server", "-b", "0.0.0.0"]entrypoint.sh#!/bin/bash
set -e
rm -f /rails-app/tmp/pids/server.pid
exec "$@"Gemfilesource "https://rubygems.org"
gem 'rails', '~> 8.0'Gemfile.lock(空ファイルを用意)ファイルを準備し、この時点で一度buildしてエラーが出ないかを確認します。
docker-compose build2, Railsの環境構築
【Railsプロジェクトを作成】
docker-compose run --rm web bundle exec rails new . --force --api --database=postgresql --skip-action-cable --skip-sprockets --skip-turbolinks --skip-webpack-install --skip-test --skip-bundle --skip-javascriptコマンド実行後、初期ファイルが生成されます。
$ docker-compose run --rm web bundle exec rails new . --force --api --database=postgresql --skip-action-cable --skip-sprockets --skip-turbolinks --skip-webpack-install --skip-test --skip-bundle --skip-javascript
[+] Creating 1/0
✔ Container docker-rails-800-db-1 Running 0.0s
Based on the specified options, the following options will also be activated:
--skip-hotwire [due to --skip-javascript]
--skip-asset-pipeline [due to --api]
exist
create README.md
create Rakefile
create .ruby-version
create config.ru
create .gitignore
create .gitattributes
force Gemfile
run git init -b main from "."
warning: re-init: ignored --initial-branch=main
Reinitialized existing Git repository in /rails-app/.git/
create app
create app/assets/stylesheets/application.css
create app/controllers/application_controller.rb
create app/helpers/application_helper.rb
create app/jobs/application_job.rb
create app/mailers/application_mailer.rb
create app/models/application_record.rb
create app/views/layouts/application.html.erbこのあと、Gemfileの設定をインストールするためにdocker-compose buildもしくは、webコンテナ内でbundle installを実行すること。
この時点でdockerを起動してRailsの初期画面が出ることを確認する。
docker-compose up3, DBの設定
config/database.ymldefault: &default
adapter: postgresql
encoding: unicode
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: <%= ENV['DB_USER'] %>
password: <%= ENV['DB_PASSWORD'] %>
host: <%= ENV['DB_HOST'] %>
port: <%= ENV.fetch("DB_PORT") { 5432 } %>
development:
<<: *default
database: rails_app_development
test:
<<: *default
database: rails_app_test
#
production:
primary: &primary_production
<<: *default
database: rails_app_production
username: rails_app
password: <%= ENV["RAILS_APP_DATABASE_PASSWORD"] %>
cache:
<<: *primary_production
database: rails_app_production_cache
migrations_paths: db/cache_migrate
queue:
<<: *primary_production
database: rails_app_production_queue
migrations_paths: db/queue_migrateデータベースが新規作成できることを確認。
docker-compose run --rm web rails db:migrate