feat(wb): pass the project's declared env vars into Docker containers#1115
Merged
Conversation
Since fnox replaced .env files, an image no longer bakes the app configuration — the age key that decrypts fnox secrets must never enter a container — so a `docker run` started by wb (e.g. `wb start --mode docker`, `wb test --e2e docker`) came up with no environment variables at all and crashed on the first value it needed. `docker run` now receives `--env <KEY>` for every variable the project declares, passing names only so the values are forwarded from wb's own environment instead of appearing in argv (`ps` output, CI logs). NODE_ENV and PORT are excluded because the image owns them: the published port mapping targets the image's port, and before fnox the baked .env files lost to the Dockerfile's `ENV` values too.
Contributor
There was a problem hiding this comment.
Code Review
This pull request extracts selectFnoxSourcedKeys into a shared utility file packages/wb/src/utils/envSources.ts and introduces a declaredEnvKeys getter on the Project class to retrieve environment variables declared by the project. Additionally, it updates the Docker run script generation to forward these declared environment variables into the container, excluding image-owned variables (NODE_ENV, PORT) and undefined values, and adds corresponding unit tests. There are no review comments, so I have no feedback to provide.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
背景
fnox 移行後、Docker イメージにはアプリの環境変数が焼き込まれません(fnox の秘密情報を復号する age 鍵をコンテナへ持ち込むことは禁止されているため)。その結果、
wbが起動するdocker run(wb start --mode dockerなど)は環境変数を一切持たないコンテナを立ち上げ、最初に必要になった値で失敗します。WillBooster/survey-system の fnox 移行(WillBooster/survey-system#538)で判明しました。変更
docker runに、プロジェクトが宣言している環境変数それぞれの--env <KEY>を渡します。名前のみを渡すため、値は wb 自身の環境から docker が引き継ぎ、コマンドライン(ps出力や CI ログ)には現れません。NODE_ENVとPORTはイメージ側が所有するため除外します(公開ポートのマッピングはイメージのポートを指しており、fnox 以前も焼き込まれた.envより Dockerfile のENVが優先されていました)。Project#declaredEnvKeysを追加。envはプロセス環境をマージしてしまうため、プロジェクト自身が宣言した変数を区別できません。selectFnoxSourcedKeysをrailwayEnv.tsからutils/envSources.tsへ移動(project.tsからコマンドモジュールを import しないため)。docker buildには何も渡していません: ビルド引数はイメージ履歴に残るため、秘密情報を焼き込まないためです。テスト
selectContainerEnvKeysのユニットテストを追加。.envを焼き込まない Dockerfile)で、ローカルビルドした wb が生成するdocker run --env <NAME> ...を実行し、コンテナが prisma のマイグレーション・シードを完了して HTTP 200 を返すことを確認しました。wb start --mode dockerが生成するコマンド文字列も期待どおり(PORTとNODE_ENVは除外)です。