LoginSignup
28
33

More than 1 year has passed since last update.

vscodeでxdebug3のdocker環境を作る

Last updated at Posted at 2021-01-29

はじめに

2020年11月25日にxdebug 3.0.0がリリースされました。
それを知らずにdockerでxdebugの環境を作って、「ブレイクポイントで止まってくれない、なんでやねん」ってなったのでまとめます。

フォルダ構成

xdebug2からxdebug3に移行のために、編集するのはphp.iniだけですがDockerfilやvscodeのlaunch.jsonの解説もします。
docker-composeは省略。

php
├── Dockerfile
└── php.ini

php.ini

xdebug3ではxdebug2と記述方法が異なるようです。

xdebug2のphp.iniのxdebugの設定

xdebug.remote_host = host.docker.internal
xdebug.remote_autostart = 1
xdebug.remote_connect_back = 0
xdebug.remote_enable = 1
xdebug.remote_handler = "dbgp"
xdebug.remote_port = 9000

xdebug3のphp.iniiのxdebugの設定

xdebug.client_host = host.docker.internal
xdebug.mode = debug
xdebug.start_with_request = yes
xdebug.discover_client_host = 0
xdebug.remote_handler = "dbgp"
xdebug.client_port = 9000
xdebug2 xdebug3の変更点
xdebug.remote_host xdebug.client_host に置き換え
xdebug.remote_autostart xdebug.mode=debug と xdebug.start_with_request=yes で設定
xdebug.remote_enable xdebug.mode=debugで設定
xdebug.remote_connect_back xdebug.discover_client_host に置き換え
xdebug.remote_handler  変更無し
xdebug.remote_port xdebug.client_port に置き換え

これら以外にも変更点があるので、詳しくはこちらを参照
https://xdebug.org/docs/upgrade_guide

Dockerfile

FROM php:7.4-fpm
SHELL ["/bin/bash","-c"]

COPY ./php.ini /usr/local/etc/php/php.ini

RUN apt-get update && \
    apt-get -y install git unzip libzip-dev libicu-dev libonig-dev && \
    pecl install xdebug && \ #xdebugのインストール
    docker-php-ext-enable xdebug  #

WORKDIR /work

ポイントはpecl install xdebugdocker-php-ext-enable xdebug

  • 2021年1月29日、現在ではpecl install xdebugを行うとxdebug3がインストールされます。

  • xdebugを使うにはイントールされた、xdebug.soのパスをphp.iniにzend_extension = パスを追記して、登録しなければなりません。docker-php-ext-enable xdebugのコマンドを叩くと自動でこの作業を行ってくれます。

launch.json

vscode側の設定になります。

"configurations":[
 {
   "name": "XDebug on docker",
   "type": "php",
   "request": "launch",
   "port": 9000,//php.iniで設定したポート番号
   "pathMappings": {
      // {docker上のdocument root}:{ローカルのdocument root}
      "/work":"${workspaceRoot}/src"
    }
  }
]

これをlaungh.jsonの中に追記します。

これでxdebugを動かすための環境ができました。

参考にさせて頂いた記事

Dockerで構築したPHP環境をxdebugでデバッグ(vscode)
Xdebug3.0.0がリリースされたので、ver2からの雑な設定コンバート

最後に

"port:9000"のポート番号はvscodeが開いてるんですね。
xdebugが開いているものと思ってました。

28
33
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
28
33