Skip to content

Docker環境でのテンプレート開発を改善する - #6712

Merged
dotani1111 merged 15 commits into
EC-CUBE:4.3from
kurozumi:local/docker-sync-automation
Apr 22, 2026
Merged

Docker環境でのテンプレート開発を改善する#6712
dotani1111 merged 15 commits into
EC-CUBE:4.3from
kurozumi:local/docker-sync-automation

Conversation

@kurozumi

@kurozumi kurozumi commented Apr 6, 2026

Copy link
Copy Markdown
Contributor

背景

Docker 環境でテンプレートをアップロードして適用しようとしても反映されないという問題があります。

原因

管理画面のテンプレート切り替えは .envECCUBE_TEMPLATE_CODE を書き込む仕組みになっています。しかし index.phpAPP_ENV が環境変数として設定されている場合、.env を一切読み込みません。

// 変更前
if (!isset($_SERVER['APP_ENV'])) {
    if (file_exists(__DIR__.'/.env')) {
        (Dotenv::createUnsafeMutable(__DIR__))->load();
    }
}

Docker 環境では docker-compose.ymlAPP_ENV: "dev" を設定しているため、.env が読み込まれず、管理画面からテンプレートを切り替えても反映されません。

変更内容

1. index.php の修正

APP_ENV が設定されている場合でも .env を読み込むように変更しました。createImmutable()->safeLoad() を使用するため、Docker で設定済みの環境変数は上書きしません。

// 変更後
} elseif (class_exists(Dotenv::class) && file_exists(__DIR__.'/.env')) {
    // APP_ENV が環境変数として設定されている場合(Docker など)でも .env を読み込む。
    // ただし既存の環境変数(Docker で設定済みのもの)は上書きしない。
    (Dotenv::createImmutable(__DIR__))->safeLoad();
}

これにより管理画面からテンプレートを切り替えると .envECCUBE_TEMPLATE_CODE が書き込まれ、次のリクエストから反映されます。Docker の再起動は不要です。

2. TemplateController に警告を追加

ECCUBE_TEMPLATE_CODE がプロセス環境変数として明示的に設定されている場合(createImmutable では上書きできないため .env の変更が反映されない)、管理画面に警告を表示します。

影響範囲

  • APP_ENV が設定されていない既存の環境には影響ありません
  • APP_ENV が設定されている環境では .env が追加で読み込まれますが、既存の環境変数は上書きされません

🤖 Generated with Claude Code

Summary by CodeRabbit

リリースノート

  • 新機能

    • テンプレートコード選択時、環境変数 ECCUBE_TEMPLATE_CODE が設定されている場合に警告メッセージを表示するようになりました。環境変数が設定されている場合は、.env ファイルの変更は適用されず、環境変数の更新が必要です。
  • 改善

    • 環境変数 .env ファイルの読み込みロジックを改善しました。外部で設定された環境変数を保持しながら、.env ファイルの値を活用できるようになりました。

docker-compose.override.yml is automatically loaded by Docker Compose
and is intended for local developer customizations. Ignoring it in git
prevents accidental commits of local-only settings.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Apr 6, 2026

Copy link
Copy Markdown

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review
📝 Walkthrough

ウォークスルー

このプルリクエストは、環境変数 ECCUBE_TEMPLATE_CODE が設定されている場合の処理を改善します。index.php では .env ファイルの読み込みロジックを拡張し、TemplateController では警告メッセージを条件付きで表示し、対応する英語と日本語の翻訳を追加しました。

変更内容

コホート / ファイル 概要
環境変数読み込みロジック
index.php
APP_ENV が既に設定されている場合、Dotenv::createImmutable().env ファイルを安全に読み込むための elseif ブランチを追加しました。
テンプレートコントローラー警告
src/Eccube/Controller/Admin/Store/TemplateController.php
ECCUBE_TEMPLATE_CODE 環境変数が設定されている場合に、admin.store.template.env_override_warning 警告メッセージを条件付きで表示するロジックを追加しました。
翻訳メッセージ
src/Eccube/Resource/locale/messages.en.yaml, src/Eccube/Resource/locale/messages.ja.yaml
環境変数オーバーライド警告用の新しいローカライゼーションキー admin.store.template.env_override_warning を英語と日本語で追加しました。

推定コードレビュー工数

🎯 2 (Simple) | ⏱️ ~15 分

🐰 テンプレートの環境、整えられて
警告メッセージで優しく導き
.env の読み込み、スムーズに続く
変数の競合、もう怖くない
ウサギも大喜び、ぴょんぴょん🎉

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Title check ✅ Passed プルリクエストのタイトル「Docker環境でのテンプレート開発を改善する」は、変更セットの主要な目的を明確に反映しており、Docker環境でのAPP_ENV設定時に.envが読み込まれるようにして、テンプレート開発を改善するという変更内容と一致している。

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

kurozumi and others added 3 commits April 6, 2026 13:22
Provides a sample override file for developers who want to mount
plugin/template directories individually for local development.
Copy to docker-compose.override.yml and customize as needed.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Provides a pre-configured override file that automatically mounts
plugin and template directories into the Docker container, so local
changes are reflected immediately without restarting the container.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@codecov

codecov Bot commented Apr 6, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 78.80%. Comparing base (1395333) to head (dc22eef).
⚠️ Report is 46 commits behind head on 4.3.

Files with missing lines Patch % Lines
...cube/Controller/Admin/Store/TemplateController.php 0.00% 2 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##                4.3    #6712      +/-   ##
============================================
+ Coverage     78.65%   78.80%   +0.14%     
- Complexity     6824     6825       +1     
============================================
  Files           476      476              
  Lines         27078    27711     +633     
============================================
+ Hits          21299    21838     +539     
- Misses         5779     5873      +94     
Flag Coverage Δ
Unit 78.80% <0.00%> (+0.14%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

kurozumi and others added 3 commits April 6, 2026 14:24
When ECCUBE_TEMPLATE_CODE is set as a process environment variable
(e.g., via Docker), writing to .env has no effect because process env
vars take precedence over .env in Symfony. Display a warning message
after saving to inform the user that the change will not be applied.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Allows developers to control the active template via the
ECCUBE_TEMPLATE_CODE environment variable. Defaults to "default".
Change the value and run docker compose restart to switch templates.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Clearer name that conveys the purpose: syncing locally developed
plugins and templates into the Docker container in real time.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@kurozumi kurozumi changed the title Add docker-compose.override.yml to .gitignore Add docker-compose.sync.yml for local plugin/template development Apr 6, 2026
@kurozumi kurozumi changed the title Add docker-compose.sync.yml for local plugin/template development Add docker-compose.sync.yml and warn when ECCUBE_TEMPLATE_CODE env var overrides .env Apr 6, 2026
…ove plugin volumes

Plugin development does not require the same workaround because
plugin state is stored in the database (named volume), not in .env.
The file is now focused solely on template development.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@kurozumi kurozumi changed the title Add docker-compose.sync.yml and warn when ECCUBE_TEMPLATE_CODE env var overrides .env Add docker-compose.template.yml and warn when ECCUBE_TEMPLATE_CODE env var overrides .env Apr 6, 2026
The previous check (getenv('ECCUBE_TEMPLATE_CODE')) was incorrect.
The real cause is that index.php skips loading .env when APP_ENV is
already set as an environment variable (e.g., via Docker), making
admin panel template changes ineffective.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@kurozumi kurozumi changed the title Add docker-compose.template.yml and warn when ECCUBE_TEMPLATE_CODE env var overrides .env Docker環境でのテンプレート開発を改善する Apr 6, 2026
@kurozumi kurozumi changed the title Docker環境でのテンプレート開発を改善する [WIP] Docker環境でのテンプレート開発を改善する Apr 6, 2026
kurozumi and others added 3 commits April 6, 2026 22:05
- Explain why .env is not loaded in Docker (APP_ENV is set)
- Document how to set ECCUBE_TEMPLATE_CODE via project root .env
- Clarify that 'up -d' is required (not restart) when changing templates

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
When APP_ENV is set as an environment variable (e.g., in Docker),
index.php previously skipped loading .env entirely, making it
impossible to switch templates via the admin panel.

With this change, .env is loaded using createImmutable()->safeLoad()
when APP_ENV is already set, so variables not defined in the Docker
environment (such as ECCUBE_TEMPLATE_CODE) are read from .env.
Existing Docker environment variables are not overridden.

This enables template switching via the admin panel in Docker without
requiring a container restart.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Now that index.php loads .env even when APP_ENV is set, template
switching via the admin panel works without requiring a dedicated
compose file or container restart.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@kurozumi kurozumi changed the title [WIP] Docker環境でのテンプレート開発を改善する Docker環境でのテンプレート開発を改善する Apr 6, 2026
Since index.php now loads .env even when APP_ENV is set,
the warning should only appear when ECCUBE_TEMPLATE_CODE is
explicitly set as a process environment variable (e.g., via Docker),
which prevents .env changes from taking effect.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@kurozumi kurozumi changed the title Docker環境でのテンプレート開発を改善する [WIP] Docker環境でのテンプレート開発を改善する Apr 7, 2026
@kurozumi

kurozumi commented Apr 7, 2026

Copy link
Copy Markdown
Contributor Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Apr 7, 2026

Copy link
Copy Markdown
✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@index.php`:
- Around line 36-40: The code currently uses
Dotenv::createImmutable(__DIR__)->safeLoad(), but createImmutable does not
include PutenvAdapter so values won't be visible via getenv() (used by
TemplateController to read ECCUBE_TEMPLATE_CODE); change the call to use
Dotenv::createUnsafeImmutable(__DIR__)->safeLoad() so the loaded .env values are
exposed to getenv() while still not overwriting existing env vars.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: d97261df-ff03-4721-836a-866c1bae2d35

📥 Commits

Reviewing files that changed from the base of the PR and between cc5a27e and 4081517.

📒 Files selected for processing (4)
  • index.php
  • src/Eccube/Controller/Admin/Store/TemplateController.php
  • src/Eccube/Resource/locale/messages.en.yaml
  • src/Eccube/Resource/locale/messages.ja.yaml
✅ Files skipped from review due to trivial changes (2)
  • src/Eccube/Resource/locale/messages.en.yaml
  • src/Eccube/Resource/locale/messages.ja.yaml
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/Eccube/Controller/Admin/Store/TemplateController.php

Comment thread index.php
ECCUBE_TEMPLATE_CODE がプロセス環境変数として設定されている場合(Docker 環境など)に
警告メッセージが表示されることを確認するテストを追加する。

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@kurozumi kurozumi changed the title [WIP] Docker環境でのテンプレート開発を改善する Docker環境でのテンプレート開発を改善する Apr 7, 2026
@dotani1111 dotani1111 added this to the 4.4.0 milestone Apr 8, 2026
@dotani1111 dotani1111 added the improvement 機能改善 label Apr 8, 2026
@dotani1111

Copy link
Copy Markdown
Contributor

@kurozumi
PRありがとうございます!

@dotani1111
dotani1111 merged commit da938cf into EC-CUBE:4.3 Apr 22, 2026
100 checks passed
dotani1111 added a commit to dotani1111/ec-cube that referenced this pull request May 11, 2026
PR EC-CUBE#6712 の `Dotenv::createImmutable($dir)->safeLoad()` 追加後, CI の
dockerbuild (admin01) が一貫して 500 になっていた. 原因は
`createImmutable` の既定の immutability check が
`ApacheAdapter / EnvConstAdapter / ServerConstAdapter` のみで,
`PutenvAdapter` を含まないこと.

CI の Apache 環境では:

- Apache の `PassEnv` は `APP_ENV APP_DEBUG TRUSTED_PROXIES TRUSTED_HOSTS`
  のみで `DATABASE_URL` を渡していない → `$_SERVER['DATABASE_URL']` 未設定
- PHP の `variables_order` が既定値 (`GPCS`) で `E` を含まない →
  `$_ENV['DATABASE_URL']` 未設定
- `getenv('DATABASE_URL')` だけが Docker から渡された postgres URL を保持

この状態で `createImmutable` の immutability check は「既存値なし」と判定し,
`.env` の `DATABASE_URL=sqlite:///var/eccube.db` を `$_SERVER` / `$_ENV` に
書き込んでしまう. Symfony の `EnvVarProcessor` は `$_SERVER` を最優先で読むため,
本来の postgres ではなく SQLite に接続してしまい, `no such table: dtb_base_info`
で 500 を引き起こしていた.

`RepositoryBuilder::createWithDefaultAdapters()->addAdapter(PutenvAdapter::class)`
で putenv 由来の値も既存値として尊重するように修正する.

- vlucas/phpdotenv v5.6.3 の挙動で確認
- 最小再現で OLD/NEW の差分検証済み (OLD は $_SERVER を sqlite で上書き,
  NEW は上書きしない)

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
dotani1111 added a commit to dotani1111/ec-cube that referenced this pull request May 11, 2026
PR EC-CUBE#6712 の `Dotenv::createImmutable($dir)->safeLoad()` 追加後, CI の
dockerbuild (admin01) が全 PHP バージョンで一貫して 500 になっていた.
原因は vlucas/phpdotenv の `createImmutable` のデフォルト adapter が
`ApacheAdapter / EnvConstAdapter / ServerConstAdapter` のみで
`PutenvAdapter` を含まないこと.

失敗経路 (CI Apache 環境):

- Apache の `PassEnv` は `APP_ENV APP_DEBUG TRUSTED_PROXIES TRUSTED_HOSTS`
  のみで `DATABASE_URL` を渡していない → `$_SERVER['DATABASE_URL']` 未設定
- PHP の `variables_order` 既定値 (`GPCS`) は `E` を含まない →
  `$_ENV['DATABASE_URL']` 未設定
- `getenv('DATABASE_URL')` だけが Docker 由来の postgres URL を保持

この状態で `createImmutable` の immutability check は「既存値なし」と判定し
`.env` の `DATABASE_URL=sqlite:///var/eccube.db` を `$_SERVER` / `$_ENV` に
書き込んでしまう. Symfony の `EnvVarProcessor` は `$_SERVER` を最優先で読むため
本来の postgres ではなく SQLite に接続し, `no such table: dtb_base_info`
で 500 を引き起こしていた.

`RepositoryBuilder::createWithDefaultAdapters()->addAdapter(PutenvAdapter::class)`
で putenv 由来の値も既存値として尊重するように修正する.

- vlucas/phpdotenv v5.6.3 の挙動で確認
- 最小再現スクリプトで OLD/NEW の差分検証済み

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
@kurozumi
kurozumi deleted the local/docker-sync-automation branch July 28, 2026 04:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

improvement 機能改善

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants