0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Railsプロジェクトをdocker化してみた。

Last updated at Posted at 2025-05-05

はじめに

今回は、Railsプロジェクトをdocker化してみました。
Dockerを学びたてということもあり、設定や構築の過程でさまざまな試行錯誤がありました。この記録が、同じように環境構築に挑戦する方の参考になれば幸いです。

🎯 目的

  • Docker と Docker Compose を使って Rails アプリをコンテナ化
  • PostgreSQL(バージョン12)を DB に使う
  • localhost:3000 で Rails にアクセスできるようにする

対象アプリはこちら:こちらのアプリをdocker化したいと思います。

🔗 https://github.com/ihatov08/rails7_docker_template

📌 公式ドキュメントを元に試してみた

まずは公式ドキュメントを参考に以下のような Dockerfile と docker-compose.yml を作成。

Dockerfile

FROM ruby:2.3.3
RUN apt-get update -qq && apt-get install -y \
  build-essential \
  libpq-dev \
  nodejs
RUN mkdir /myapp
WORKDIR /myapp
COPY Gemfile Gemfile.lock ./
RUN bundle install
COPY . .

docker-compose.yml

version: "3"
services:
  db:
    image: postgres
  web:
    build: .
    command: bundle exec rails s -p 3000 -b '0.0.0.0'
    volumes:
      - .:/myapp
    ports:
      - "3000:3000"
    depends_on:
      - db

🐛 最初のつまづき:apt-getエラー

docker-compose up で次のようなエラーが発生:

E: Some index files failed to download. They have been ignored, or old ones used instead.

→ 原因はベースイメージが古く、パッケージのインデックスが取得できなかったこと。
→ Gemfileを見たところruby "3.2.2"という表記があったのでRuby3.2.2に変更。さらにbundler を明示インストール

Dockerfile

FROM ruby:3.2.2 `ここを変更`
RUN apt-get update -qq && apt-get install -y \
  build-essential \
  libpq-dev \
  nodejs
RUN mkdir /myapp
WORKDIR /myapp
COPY Gemfile Gemfile.lock ./
RUN gem install bundler:2.4.10`ここを変更`
RUN bundle install
COPY . .

🐛 localhost:3000 へのアクセスは成功、、、しかし!

ブラウザで http://localhost:3000 にアクセスはできたが、データベース接続に失敗。

スクリーンショット 2025-05-05 11.19.30.png

DBの設定を何も記述していなかったことが原因!なのでDB周りの設定を記述

✅ 解決策:環境変数と設定を調整

config/database.yml を修正:

default: &default
  adapter: postgresql
  encoding: unicode
  host: db
  username: postgres
  password: <%= ENV.fetch("DATABASE_PASSWORD") %>
  port: 5432
  pool: 5

development:
  <<: *default
  database: myapp_development

docker-compose.yml も修正:

version: "3"

volumes: //ここを追記
  db-data:

services:
  db: //以下を追記
    image: postgres:12
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: password
    volumes:
      - "db-data:/var/lib/postgresql/data"

  web:
    build: .
    command: bundle exec rails s -p 3000 -b '0.0.0.0'
    volumes:
      - .:/myapp
    ports:
      - "3000:3000"
    depends_on:
      - db
    environment:      //ここを追記
      DATABASE_PASSWORD: password

✅ 最終結果:正常に起動!

docker-compose build
docker-compose up
http://localhost:3000 にアクセス⇩⇩
スクリーンショット 2025-05-05 12.17.39.png

Rails の初期画面を確認 ✅
PostgreSQL にも正常に接続 ✅

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?