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?

Docker×Railsをgit cloneしたときのセットアップ手順

Posted at

はじめに

Railsを使った開発を以下のように進めていたのですが、railsコマンドが遅すぎたので、WSL上にプロジェクトファイルを移しました。

  • Dockerを使って開発
  • プロジェクトファイルはWindows上においていた

少し詰まったのと今後も似たようなことをやることが多くなりそうなので、備忘録として残します。

開発環境

  • Rails8
  • Ruby3.4
  • Docker27.2.0

やりたいこと

やりかた

  1. WSL上にディレクトリを作成
  2. git cloneを実行
  3. docker compose buildでイメージをビルド
  4. docker compose up -d でコンテナを立ち上げ
  5. docker compose exec web /bin/bash でrails内に入る
    1. bin/rails db:create などの実行
    2. bin/dev を実行
      1. scssを反映させるために、devコマンドでdartsassによるコンパイルを実行

出たエラー

Rails用のコンテナが立ち上がらない

  • エラー文
web-1  | tail: cannot open 'log/development.log' for reading: No such file or directory
web-1  | tail: no files remaining
  • 前提:compose.yamlの記述内容
services:
  db:
    image: postgres
    environment:
      POSTGRES_USER: user
      POSTGRES_PASSWORD: password
    ports:
      - "5432:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data
  web:
    build:
      context: .
      dockerfile: Dockerfile.development
    command: bash -c "rm -f tmp/pids/server.pid && tail -f log/development.log" // ここでエラー
    volumes:
      - .:/myapp
    ports:
      - "3000:3000"
    depends_on:
      - db
    tty: true
    stdin_open: true
volumes:
  postgres_data:
  • 原因
    • git cloneで初期化下手だったのでlog/development.logが存在しなかった
  • 背景
    • よく見る起動コマンドには rails s -b ‘0.0.0.0’が含まれているが、サーバーを立ち上げたり落としたりを頻繁にしたいので、抜いていた
    • 上記のコマンドを削除すると、自動的にコンテナが終了してしまうので、ログを参照するコマンドを入れていた
  • 対処法
    1. tail -f log/development.lograils s -b ‘0.0.0.0’に変更
    2. docker compose up -dで再起動
    3. docker compose exec web /bin/bash でコンテナ内に入る
    4. bundle installbin/rails db:create, bin/rails db:migrate, bin/rails db:seed などの実行
    5. log/development.logが自動で生成されるので、compose.yamlファイルを元に戻す
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?