LoginSignup
289
243

More than 5 years have passed since last update.

foreman で アプリケーションを動かす。

Last updated at Posted at 2014-09-14

foreman とは

foreman は、Procfile を読み込み、複数のプロセスを管理できるツール。
Heroku を動かす際にも用いられている。
(Heroku toolbelt インストール時に自動的にインストールされる。)

または、gem でインストールする。

$ gem install foreman

使い方

$ foreman -h
Commands:
  foreman check                   # Validate your application's Procfile
  foreman export FORMAT LOCATION  # Export the application to another proce...
  foreman help [COMMAND]          # Describe available commands or one spec...
  foreman run COMMAND [ARGS...]   # Run a command using your application's ...
  foreman start [PROCESS]         # Start the application (or a specific PR...
  foreman version                 # Display Foreman gem version

Options:
  -f, [--procfile=PROCFILE]  # Default: Procfile
  -d, [--root=ROOT]          # Default: Procfile directory

Procfile を配置

アプリケーションで管理するプロセスを Procfile に記述し、アプリケーションルートに配置。

Procfile の書き方

<process type>: <command>

process type : web, worker, clock などが代表的で、名前はなんでもよい。
(Heroku では web は特別で、この名前が Heroku ルータからの HTTP を受け付ける。)

Procfile
web: bundle exec rails server -p $PORT  # rails 起動($PORTは5000がデフォ)
worker: bundle exec rake jobs:worK      # バッチ起動
redis: bundle exec redis-server /usr/local/etc/redis.conf  # Redis 起動

確認

Procfile の設定が有効かを確かめる

$ foreman check

起動

$ foreman start

これで、Procfile に設定したすべてのプロセスを一度に起動することができる。
終了は Ctrl + c

オプション

-f

環境ごとに Procfile を変えたいときは -f オプションでファイルを指定する。

$ foreman start -f Procfile.dev

-c

並列して実行するプロセス数を指定する。

$ foreman start -c worker=2

-p

ポートを指定して起動する(デフォルトでは $PORT は 5000から)

$ foreman start -p 3000

-d

ディレクトリを指定できるっぽいけど、ほとんど使わないと思う。

-e (.env)

.env を読み込んで起動。
環境変数とかをここ宣言しておくと便利。

.env
APP_ID="xxxxxxxxx"

設定した環境変数は、rubyであれば

$ foreman run pry
[1] pry(main)> puts ENV["APP_ID"]
xxxxxxxxx
=> nil

で確認できる。

.foreman

.foreman
port: 3000

foreman コマンドのデフォルトオプション指定などは、.foremanファイルに記述することで、起動時に自動的に設定できる。

※これらのファイルは .gitignore に設定してコミットしないよう気をつける。

参考・引用

289
243
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
289
243