Railsのキー設定でつまづいた話
目次
前置き
前回は、Docker-compose + Rails + PostgreSQL で環境構築までしました。
今回はviewファイルを作ってhttp://localhost:3000
にアクセスして表示されてるか確認しようとしたところで、エラー発生。
本編
$ docker-compose up
を使ってサーバーを起動して...
http://localhost:3000
にアクセスしよっと。
ん?今エラーが見えたな...うわぁ...
rails-1 | /usr/local/bundle/ruby/3.3.0/gems/railties-7.2.1/lib/rails/application/configuration.rb:519:in `secret_key_base=': Missing `secret_key_base` for 'production' environment, set this string with `bin/rails credentials:edit` (ArgumentError)
rails-1 |
rails-1 | raise ArgumentError, "Missing `secret_key_base` for '#{Rails.env}' environment, set this string with `bin/rails credentials:edit`"
rails-1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
なんか、secret_key_baseってやつがいるんか。ナニソレ🙃
credentialsってファイルがあるんかな?vscodeでファイル検索してみよか。
見つかった〜。ん?変な文字列書いてる...中身何書けばええんやろ
aws:
access_key_id: *****************
secret_access_key: *****************
secret_key_base: ***************************************************
こんな感じに書けばええんか。
シークレットキーの作成方法は$ rails secret
を実行して新しいsecret_key_baseを生成するんか。
書き換えたし、動くかな? エラー文が変わらない...
credentials.yml.enc について調べてみよう。
ファイルを直接編集することはできない
まじか。じゃあ、今までvscodeで書いたのは間違いやったんか。変な文字列って暗号化されたやつか。
ん?エラー文に開き方書いてるやん。
$ EDITOR="nano" bin/rails credentials:edit
じゃあ、これでファイルを開けて。secret_key_base:
の欄にさっきのシークレットキーをコピペして...実行!
エラー文が変わらない。
production環境への変え方が分からない。
aws:
access_key_id: *****************
secret_access_key: *****************
production:
secret_key_base: ***************************************************
にしてみた。ダメそう。
調べているとmaster_keyの話とか出てきた。ほんとに必要?
分からなかったので、ヘルプを出しました。
secret_key_baseはcredentials.yml.enc
とproduction.yml.enc
両方とも設定できていました。
問題はDockerfileだったみたい。
ENV RAILS_ENV="production" \
という欄を
ENV RAILS_ENV="development" \
に変更。
そして、dockerのイメージをビルドし直して、サーバー起動。
docker-compose build
docker-compose up
WARN[0000] Found orphan containers ([todo_app-rails-run-d75ed8dc5d6c todo_app-rails-run-ffed41f3dc6c todo_app-rails-run-fd6f8b462beb todo_app-rails-run-94547ea75f1d todo_app-rails-run-cd4e3a7e670b todo_app-rails-run-7c0b1cf0389d todo_app-rails-run-32580694a48b todo_app-rails-run-53b7c441e82e todo_app-web-run-fe28117b1757 todo_app-web-run-39b6523d18b5]) for this project. If you removed or renamed this service in your compose file, you can run this command with the --remove-orphans flag to clean it up.
[+] Running 2/0
✔ Container todo_app-db-1 Created 0.0s
✔ Container todo_app-rails-1 Recreated 0.1s
Attaching to db-1, rails-1
db-1 |
db-1 | PostgreSQL Database directory appears to contain a database; Skipping initialization
db-1 |
db-1 | 2024-09-03 06:24:13.373 UTC [1] LOG: starting PostgreSQL 16.4 on aarch64-unknown-linux-musl, compiled by gcc (Alpine 13.2.1_git20240309) 13.2.1 20240309, 64-bit
db-1 | 2024-09-03 06:24:13.373 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
db-1 | 2024-09-03 06:24:13.373 UTC [1] LOG: listening on IPv6 address "::", port 5432
db-1 | 2024-09-03 06:24:13.374 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db-1 | 2024-09-03 06:24:13.376 UTC [29] LOG: database system was shut down at 2024-09-03 06:23:04 UTC
db-1 | 2024-09-03 06:24:13.378 UTC [1] LOG: database system is ready to accept connections
rails-1 | => Booting Puma
rails-1 | => Rails 7.2.1 application starting in development
rails-1 | => Run `bin/rails server --help` for more startup options
rails-1 | Puma starting in single mode...
rails-1 | * Puma version: 6.4.2 (ruby 3.3.4-p94) ("The Eagle of Durango")
rails-1 | * Min threads: 3
rails-1 | * Max threads: 3
rails-1 | * Environment: development
rails-1 | * PID: 1
rails-1 | * Listening on http://0.0.0.0:3000
rails-1 | Use Ctrl-C to stop
db-1 | 2024-09-03 06:29:13.461 UTC [27] LOG: checkpoint starting: time
db-1 | 2024-09-03 06:29:13.479 UTC [27] LOG: checkpoint complete: wrote 3 buffers (0.0%); 0 WAL file(s) added, 0 removed, 0 recycled; write=0.005 s, sync=0.002 s, total=0.019 s; sync files=2, longest=0.002 s, average=0.001 s; distance=0 kB, estimate=0 kB; lsn=0/154E3E0, redo lsn=0/154E3A8
多分できた!!
要約
rails-1 | /usr/local/bundle/ruby/3.3.0/gems/railties-7.2.1/lib/rails/application/configuration.rb:519:in `secret_key_base=': Missing `secret_key_base` for 'production' environment, set this string with `bin/rails credentials:edit` (ArgumentError)
rails-1 |
rails-1 | raise ArgumentError, "Missing `secret_key_base` for '#{Rails.env}' environment, set this string with `bin/rails credentials:edit`"
rails-1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
これは、production環境でsecret_key_baseがないって話をしています。
エラー文に書いてある通り、以下のコマンドを打ちましょう。
credentials.yml.encは暗号化されているので、直接編集できません。なので、わざわざコマンドを打ちます。
$ EDITOR="nano" bin/rails credentials:edit
これでcredentials.ymlの中身が見られて、secret_key_baseの欄にキーが設定されているか確認してください。
されていなかった場合は、乱雑に入力して決めてもいいですし、丁寧にやりたい方は以下のコマンドをターミナルに入力してください。
$ rails secret
このコマンドを打つことにより、シークレットキーが作成されます。
作成されたキーを先程のsecret_key_baseの欄にコピペしてください。
編集が終わったらctrl
+X
→ yes
→ enter
で保存してください。
secret_key_baseが設定されているか読み取るためには以下のコマンドを入力してください。
$ rails c
irb(main):001:0> Rails.application.credentials.aws[:access_key_id]
=> 123
irb(main):002:0> Rails.application.credentials.aws[:secret_access_key]
=> 345
irb(main):003:0> Rails.application.credentials.secret_key_base
=> "8be8e637d755f79c799048bed8be0c..."
irb(main):004:0> exit
secret_key_baseだけ表示されてない場合は、こちらの記事おすすめです。
次にDockerfile内を以下のように編集してください。
(元々こう書かれている人は駄文しなくても大丈夫です。)
ENV RAILS_ENV="development" \
そして、dockerのイメージをビルドし直して、サーバーを起動してください。
docker-compose build
docker-compose up
自分の場合はこれで解決しました。