本教程演示了如何使用环境变量在Dockerized PHP 8.4应用程序中配置Xdebug,从而增强了灵活性并避免了硬编码INI设置。 以前的教程硬编码Xdebug设置,限制开发人员自定义。这种方法允许每个开发人员在不更改docker映像的情况下管理其XDEBUG配置。
使用环境变量简化配置,使用环境变量支持INI文件中的后备值。 以前,>文件看起来像这样:xdebug.ini
<code>; build/php/conf.d/xdebug.ini file [xdebug] xdebug.mode = debug xdebug.client_host = host.docker.internal ; Or use the host machine IP address: ; xdebug.client_host = 192.168.86.203 xdebug.start_with_request = yes</code>此方法需要每个开发人员的手动INI文件更新,这是效率低下的。 优越的方法利用环境变量:
<code>services: app: # ... other configurations ... environment: XDEBUG_CONFIG: "client_host=0.0.0.0 start_with_request=yes" XDEBUG_MODE: "debug,develop"</code>>将这些环境变量存储在未转化的
文件中允许每次开发器自定义。 另外,您可以直接在.docker.env
>文件中直接利用环境变量:xdebug.ini
<code>; build/php/conf.d/xdebug.ini file [xdebug] xdebug.mode = ${PHP_XDEBUG_MODE:-debug,develop} xdebug.client_host = ${PHP_XDEBUG_CLIENT_HOST:-host.docker.internal} xdebug.start_with_request = ${PHP_XDEBUG_START_WITH_REQUEST:-trigger}</code>>和
保留用于直接Xdebug配置。PHP_
>
XDEBUG_MODE
>要验证设置,将XDEBUG_CONFIG
添加到
>
phpinfo(); exit;
public/index.php
对于本地自定义,将一个
$ docker compose up --build -d $ docker compose exec app bash $ php -i | grep xdebug\.start_with_request>:
>
env_file
docker-compose.yaml
create
services: app: # ... other configurations ... env_file: - .docker.env>)和
.docker.env
具有默认设置的文件:.gitignore
.docker.env.example
<code>PHP_XDEBUG_MODE=debug PHP_XDEBUG_CLIENT_HOST=host.docker.internal # Or use your computer's local network IP # PHP_XDEBUG_CLIENT_HOST=192.168.86.250 PHP_XDEBUG_START_WITH_REQUEST=trigger</code>>中可见:
phpinfo()
此方法允许在不修改图像构建过程的情况下进行灵活的Xdebug配置。 虽然直接的Xdebug环境变量提供了简单性,但INI方法提供了更全面的控制,因为并非所有设置都可以通过
以上是具有PHP INI环境变量的灵活Docker图像的详细内容。更多信息请关注PHP中文网其他相关文章!