September 05, 2018
PhpStormでステップ実行でのデバッグをするための解説です。 度々忘れるので記事にします。
動作環境
Xdebugの設定
PhpStormの設定
PHPが動作するコンテナのDockerfileの例
FROM php:7.2-fpm | |
... 省略 | |
RUN pecl install xdebug && \ | |
docker-php-ext-enable xdebug | |
... 省略 | |
COPY ./xdebug.ini /usr/local/etc/php/conf.d/xdebug.ini | |
... 省略 | |
EXPOSE 9000 |
xdebug.iniの例
xdebug.remote_host=host.docker.internal | |
xdebug.remote_connect_back=0 | |
xdebug.remote_port=9000 | |
xdebug.idekey=PHPSTORM | |
xdebug.remote_autostart=1 | |
xdebug.remote_enable=1 | |
xdebug.cli_color=0 | |
xdebug.profiler_enable=0 | |
xdebug.profiler_output_dir="~/xdebug/phpstorm/tmp/profiling" | |
xdebug.remote_handler=dbgp | |
xdebug.remote_mode=req | |
xdebug.var_display_max_children=-1 | |
xdebug.var_display_max_data=-1 | |
xdebug.var_display_max_depth=-1 |
以下のサイトに全ての項目ではありませんが、各項目の分かりやすい説明がありました。 https://qiita.com/castaneai/items/d5fdf577a348012ed8af
xdebug.remote_hostを少し解説します。
デバッグの制御を開始するには、PHP動作側とリモートをつなぐ必要があります。PHP動作側(つまりコンテナ)からホスト(Mac、PhpStorm)に接続したいわけです。
remote_hostに設定する値は、ホストのIPを名前解決する値を指定するわけですが、その値は host.docker.internal
としてdockerが用意してくれています。
The host has a changing IP address (or none if you have no network access). From 18.03 onwards our recommendation is to connect to the special DNS name host.docker.internal, which resolves to the internal IP address used by the host. This is for development purpose and will not work in a production environment outside of Docker for Mac.
https://docs.docker.com/docker-for-mac/networking/#i-cannot-ping-my-containers
こちらの値でも接続できるはずですが、そのうち使えなくなります。
以上になります。
Written by Ta Toshio who lives and works in Saitama, Japan .You should follow him on Twitter