rails6 foremanを導入する

rails6 foremanを導入する

rails6で、Webpackerを毎回ビルドするのが、面倒だったのでforemanを導入する手順を記述してます。railsのバージョンは6.1.0です。

環境

  • OS ubuntu20.10
  • ruby 2.7.2
  • rails 6.1.0
  • Postgresql 13.1 (dockerで構築)

foreman導入

開発用でしか使用しないので、「Gemfile」に以下のように記述します。

gem "foreman", group: :development

インストールします。

bundle install

設定ファイル作成

デフォルトでは「Procfile」というファイルを読みに行くので、プロジェクト配下に「Procfile」を作成します。

ここでは、以下のように記述してます。

rails: rails s -b 0.0.0.0
webpacker: ./bin/webpack-dev-server

※bin/webpack-dev-serverは、サーバー上での自動ビルドが可能となってます。

起動

実行すると、デフォルトのポート番号「5000」でサーバーが起動されます。

bundle exec foreman start

<出力結果>
16:38:06 rails.1     | started with pid 162373
16:38:06 webpacker.1 | started with pid 162374
16:38:29 rails.1     | => Booting Puma
16:38:29 rails.1     | => Rails 6.1.0 application starting in development
16:38:29 rails.1     | => Run `bin/rails server --help` for more startup options
16:38:32 rails.1     | Puma starting in single mode...
16:38:32 rails.1     | * Puma version: 5.1.1 (ruby 2.7.2-p137) ("At Your Service")
16:38:32 rails.1     | *  Min threads: 5
16:38:32 rails.1     | *  Max threads: 5
16:38:32 rails.1     | *  Environment: development
16:38:32 rails.1     | *          PID: 162373
16:38:32 rails.1     | * Listening on http://0.0.0.0:5000
16:38:33 rails.1     | Use Ctrl-C to stop
16:38:36 webpacker.1 | ℹ 「wds」: Project is running at http://localhost:3035/
16:38:36 webpacker.1 | ℹ 「wds」: webpack output is served from /packs/
16:38:36 webpacker.1 | ℹ 「wds」: Content not from webpack is served from /home/mebee/rails-test/test-project/public/packs
16:38:36 webpacker.1 | ℹ 「wds」: 404s will fallback to /index.html
16:39:00 webpacker.1 | ℹ 「wdm」: Hash: d58b2bc701989f3589b7
16:39:00 webpacker.1 | Version: webpack 4.44.2
16:39:00 webpacker.1 | Time: 23294ms
16:39:00 webpacker.1 | Built at: 202x/xx/xx 16:38:59
16:39:00 webpacker.1 |                                      Asset       Size       Chunks                         Chunk Names
16:39:00 webpacker.1 |     js/application-67d76c3be95e47239efd.js    509 KiB  application  [emitted] [immutable]  application
16:39:00 webpacker.1 | js/application-67d76c3be95e47239efd.js.map    573 KiB  application  [emitted] [dev]        application
16:39:00 webpacker.1 |       js/hello_vue-a915e2c4c5ecb7c79a0b.js    650 KiB    hello_vue  [emitted] [immutable]  hello_vue
16:39:00 webpacker.1 |   js/hello_vue-a915e2c4c5ecb7c79a0b.js.map    745 KiB    hello_vue  [emitted] [dev]        hello_vue
16:39:00 webpacker.1 |                              manifest.json  689 bytes               [emitted]
16:39:00 webpacker.1 | ℹ 「wdm」: Compiled successfully.

ブラウザから http://プライベートIP or サーバーアドレス:5000 にアクセスできることが確認できると思います。

ポート変更

実行するポートは、以下で変更可能です。

bundle exec foreman start -p 3000

「Procfile」を編集しても同じです。

rails: rails s -b 0.0.0.0 -p 3000

設定確認

設定は「check」で確認できます。

bundle exec foreman check

<出力結果>
valid procfile detected (rails, webpacker)

別の設定ファイルを読み込み

別の設定ファイルを読み込む場合は「-f」を使用します。

bundle exec foreman start -f Procfile.dev