実現したいこと
php:7.4-fpm-busterにxdebugを入れて、phpファイルをデバッグできるようにしたい
発生している問題・分からないこと
ブレイクポイントが無効になり止まりません。
launch.json
{ "version": "0.2.0", "configurations": [ { "name": "XDebug ", "type": "php", "request": "launch", "port": 9003, "stopOnEntry": false, "pathMappings": { "/var/www/hoge": "${workspaceFolder}/foo/hoge-backend" }, "log": true } ] }
php.ini
xdebug.mode=debug xdebug.start_with_request=yes ; xdebug.client_host=host.docker.internal xdebug.client_host=xxx.xxx.xx.xx※host.docker.internalで繋がらないため、ip固定 xdebug.client_port=9003 xdebug.log=/var/log/xdebug.log xdebug.log_level = 10
docker-compose.yml
nginx image: nginx container_name: nginx ports: - "80:80" - "443:443" volumes: - ./foo/hoge/public:/var/www/hoge/public - ./foo/nginx/local/default.conf:/etc/nginx/conf.d/default.conf depends_on: - api networks:
Dockerfile
FROM php:7.4-fpm-buster RUN docker-php-ext-enable xdebug \ && touch /var/log/xdebug.log && chmod a+w /var/log/xdebug.log
xdebug.logのコマンドのエラー内容
[8] [Step Debug] DEBUG: I: Matching breakpoint '/var/www/foo/app/Http/Controllers/User/UserController.php:64' against location '/usr/local/lib/php/PEAR/XMLParser.php:195'. [8] [Step Debug] DEBUG: R: File name length (37) doesn't
vscodeのデバックコンソールのログです
<- outputEvent Nb { seq: 0, type: 'event', event: 'output', body: { category: 'stdout', output: 'Listening to Xdebug on port 0.0.0.0:9003,:::9003 ...\n' } } Listening to Xdebug on port 0.0.0.0:9003,:::9003 ... <- launchResponse mb { seq: 0, type: 'response', request_seq: 2, command: 'launch', success: true }
vsxcodeのエラーメッセージが表示されています。
'PHP Xdebug' 拡張機能を有効にできません。それが不明な 'felixfbecker.php-debug' 拡張機能に依存しているためです。
Unknown sourceReference 0
補足
【環境】
Docker+Laravel
※WSLは使わずMacでDockerを使用しています
※以下同じエラーがありましたので参考までに
https://stackoverflow.com/questions/70361039/vscode-hits-breakpoints-then-stops
アドバイスの対応後とそのエラー①:解決済み
エラーのため、xdebug.iniにxdebug.client_hostとxdebug.client_port を追加
※Xdebugの起動漏れでした
WARN: Creating socket for 'xxx.xxx.xx.xx:9003', poll success, but error: Operation now in progress (29). ERR: Could not connect to debugging client. Tried: xxx.xxx.xx.xx:9003
cat > docker-php-ext-xdebug.ini <<EOF zend_extension=xdebug xdebug.mode=debug xdebug.log=/var/log/xdebug.log xdebug.log_level=10 xdebug.start_with_request=yes EOF 追加してもエラー発生 xdebug.client_host = xxx.xxx.xx.xx xdebug.client_port = 9003
docker run -i --rm --name xdebug \ -v $(pwd)/docker-php-ext-xdebug.ini:/usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \ -v $(pwd)/html:/var/www/html xdebug bash -x <<EOF & sleep 20 php test.php cat /var/log/xdebug.log EOF
アドバイスの対応後とそのエラー②:エラー中
[8] [Step Debug]DEBUG: Checking whether to break on ファイル名 [8] [Step Debug] DEBUG: I: Current location: /var/www/html/test.php:2. [8] [Step Debug] DEBUG: I: Matching breakpoint ファイル名 [8] [Step Debug] DEBUG: R: File name length (22) doesn't match with breakpoint ファイル名
Dockerfileの中身がわからないと再現できないかと思いますので追記されたほうが良いかと思います。
stopOnEntryがtrueの場合に最初は停止するようでしたら、pathMappingsに誤りがある可能性が高いです。
コンテナ内のLaravelディレクトリは /var/www/hoge なのですか?
コメントありがとうございます。
Dockerfileを追記しました。
stopOnEntryがtrueの場合にブレイクポイントとは関係なく、何度も停止します。
コンテナ内のLaravelディレクトリは間違ってないと思います。
(GPT確認行いました)

laravelは何の関係もないと思うので、最小限の構成で質問しましょう。
つまりphp:7.4-fpm-busterにxdebugを入れて、単品のphpファイルをデバッグできるようになるまでをゴールとした質問をしてください。●●●●
●●●●
コメントありがとうございます。
実現したいことを更新しました。
必要な情報がかなり足りない上
→抽象的ではなく、どの情報が必要か教えていただけますか?
共有できる内容はできる限り共有します。
このコメントだけだと解決には進まないので。

●●●●
そもそも実現したいことをする設定に全くなっていないし、足りない情報が全く追加されていません
そのファイルが何なのか、何かを実行した結果何が出力されたのか、具体的にどういう操作をして、具体的に何が起きたのか記載がありません
●●●●

●●●●実現したいことは以下で出来ますよ。
cat >Dockerfile <<EOF
FROM php:7.4-fpm-buster
RUN pecl install xdebug-3.1.6 && docker-php-ext-enable xdebug
EOF
docker build -t hoge .
cat >docker-php-ext-xdebug.ini <<EOF
zend_extension=xdebug
xdebug.mode=debug
xdebug.log=/var/log/xdebug.log
xdebug.log_level=10
xdebug.start_with_request=yes
EOF
mkdir html
cat >html/test.php <<EOF
<?php
\$a=1;
\$b=2;
?>
EOF
docker run -i --rm --name hoge -v $(pwd)/docker-php-ext-xdebug.ini:/usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini -v $(pwd)/html:/var/www/html hoge bash -x <<EOF &
sleep 20
php test.php
cat /var/log/xdebug.log
EOF
sleep 5
docker exec -i hoge bash -x<<EOF
# ここはx86_64(amd64)用なので必要なバイナリに変えること
# https://xdebug.org/download#dbgpClient
curl -o dbgpClient 'https://xdebug.org/files/binaries/dbgpClient'
chmod u+x ./dbgpClient
./dbgpClient <<EOF2
breakpoint_set -t line -f test.php -n 3
run
context_get
run
EOF2
EOF
wait
※環境によってsleepの時間調整とcurlで取ってくるファイルを変える必要があります
全く分かってなくてすいません。
既存の設定編集ではなく、別途イメージ作成から行ってxdebugとの接続を行うってことですね?
curlで取ってくるファイルを変える必要があります
→ここについてもう少し詳しく教えていただけますか?

●●●●

●●●●

一応先のスクリプトの実行結果だけ貼っておきます。
$ sh -x ../hoge.sh
+ cat
+ docker build -t hoge .
[+] Building 6.6s (6/6) FINISHED docker:default
=> [internal] load build definition from Dockerfile 0.5s
=> => transferring dockerfile: 123B 0.0s
=> [internal] load metadata for docker.io/library/php:7.4-fpm-buster 1.6s
=> [internal] load .dockerignore 0.5s
=> => transferring context: 2B 0.0s
=> [1/2] FROM docker.io/library/php:7.4-fpm-buster@sha256:f7c4f7267af327700c172034c2a9a354c85c86fadbe412cd11e709a37e475bda 0.0s
=> CACHED [2/2] RUN pecl install xdebug-3.1.6 && docker-php-ext-enable xdebug 0.0s
=> exporting to image 0.5s
=> => exporting layers 0.0s
=> => writing image sha256:21477e9cdf863f918af366f947bbcf09e6e840034a75b8b8659994b0f772dff7 0.2s
=> => naming to docker.io/library/hoge 0.2s
+ cat
+ mkdir html
+ cat
+ sleep 5
+ pwd
+ pwd
+ docker run -i --rm --name hoge -v /xxx/docker-php-ext-xdebug.ini:/usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini -v /xxx/html:/var/www/html hoge bash -x
+ docker exec -i hoge bash -x
+ sleep 20
+ curl -o dbgpClient https://xdebug.org/files/binaries/dbgpClient
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 7237k 100 7237k 0 0 1997k 0 0:00:03 0:00:03 --:--:-- 1997k
+ chmod u+x ./dbgpClient
+ ./dbgpClient
Xdebug Simple DBGp client (0.6.1)
Copyright 2019-2024 by Derick Rethans
Waiting for debug server to connect on port 9003.
+ php test.php
Connect from [::1]:53932
DBGp/1.0: Xdebug 3.1.6 — For PHP 7.4.33
Debugging file:///var/www/html/test.php (ID: 23/)
1 | breakpoint_set
1 | Breakpoint set with ID 230001
2 | run > break/ok
2 | file:///var/www/html/test.php:3
3 | context_get
3 | int $a: 1
3 | uninitialized $b
4 | run > stopping/ok
Error while handling connection: EOF
Disconnect
+ cat /var/log/xdebug.log
[23] Log opened at 2024-07-26 09:24:34.713538
[23] [Step Debug] INFO: Connecting to configured address/port: localhost:9003.
[23] [Step Debug] INFO: Connected to debugging client: localhost:9003 (through xdebug.client_host/xdebug.client_port). :-)
[23] [Step Debug] -> <init xmlns="urn:debugger_protocol_v1" xmlns:xdebug="https://xdebug.org/dbgp/xdebug" fileuri="file:///var/www/html/test.php" language="PHP" xdebug:language_version="7.4.33" protocol_version="1.0" appid="23"><engine version="3.1.6"><![CDATA[Xdebug]]></engine><author><![CDATA[Derick Rethans]]></author><url><![CDATA[https://xdebug.org]]></url><copyright><![CDATA[Copyright (c) 2002-2022 by Derick Rethans]]></copyright></init>
[23] [Step Debug] <- breakpoint_set -i 1 -t line -f test.php -n 3
[23] [Step Debug] -> <response xmlns="urn:debugger_protocol_v1" xmlns:xdebug="https://xdebug.org/dbgp/xdebug" command="breakpoint_set" transaction_id="1" id="230001"></response>
[23] [Step Debug] <- run -i 2
[23] [Step Debug] DEBUG: Checking whether to break on /var/www/html/test.php:3.
[23] [Step Debug] DEBUG: I: Current location: /var/www/html/test.php:2.
[23] [Step Debug] DEBUG: I: Matching breakpoint '/var/www/html/test.php:3' against location '/var/www/html/test.php:2'.
[23] [Step Debug] DEBUG: R: Line number (2) doesn't match with breakpoint (3).
[23] [Step Debug] DEBUG: Checking whether to break on /var/www/html/test.php:3.
[23] [Step Debug] DEBUG: I: Current location: /var/www/html/test.php:3.
[23] [Step Debug] DEBUG: I: Matching breakpoint '/var/www/html/test.php:3' against location '/var/www/html/test.php:3'.
[23] [Step Debug] DEBUG: F: File names match (/var/www/html/test.php).
[23] [Step Debug] -> <response xmlns="urn:debugger_protocol_v1" xmlns:xdebug="https://xdebug.org/dbgp/xdebug" command="run" transaction_id="2" status="break" reason="ok"><xdebug:message filename="file:///var/www/html/test.php" lineno="3"></xdebug:message></response>
[23] [Step Debug] <- context_get -i 3
[23] [Step Debug] -> <response xmlns="urn:debugger_protocol_v1" xmlns:xdebug="https://xdebug.org/dbgp/xdebug" command="context_get" transaction_id="3" context="0"><property name="$a" fullname="$a" type="int"><![CDATA[1]]></property><property name="$b" fullname="$b" type="uninitialized"></property></response>
[23] [Step Debug] <- run -i 4
[23] [Step Debug] -> <response xmlns="urn:debugger_protocol_v1" xmlns:xdebug="https://xdebug.org/dbgp/xdebug" command="run" transaction_id="4" status="stopping" reason="ok"></response>
[23] Log closed at 2024-07-26 09:24:34.726679
+ wait
$
記載された内容だけだと、誰も状況が再現ができないので回答はつきづらいと思います。
現状だと、ログを出すようにして頑張って調べて、くらいしか言いようがないです。
Dockerfile は動作するものを記載してください。抜粋とかかれてますが、ビルドエラーになります。他の部分の記述によって動作が変わってきます。
Webサーバーとして動作させるならnginxかapacheのコンテナもあるのではないでしょうか?また、ビルドや起動した際のコマンドか、docker composeを利用しているのであればymlも必要です。
パスとか設定は全然違ってて良くて動くものなら回答をだせますが、それだと質問者さんは何がどう違うか理解できなくて自分の環境を解決できないでしょう。
わかりました。
ありがとうございます。
今回はパスが違ってもブレイクポイントで止まるようにしたい方法を教えていただけないでしょうか?
xdebugというコンテナ、イメージ名になっています
```
cat > docker-php-ext-xdebug.ini <<EOF
zend_extension=xdebug
xdebug.mode=debug
xdebug.log=/var/log/xdebug.log
xdebug.log_level=10
xdebug.start_with_request=yes
EOF
```
```
docker run -i --rm --name xdebug \
-v $(pwd)/docker-php-ext-xdebug.ini:/usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
-v $(pwd)/html:/var/www/html xdebug bash -x <<EOF &
sleep 20
php test.php
cat /var/log/xdebug.log
EOF
```
webサーバーは立ててないのですか?
デバッグする以前にページが表示できないのでは
xdebugが起動していないだけでした。
xdebug.iniを作成する前と同じようなのエラーが発生しています

@mita0
ハッキリ言っておきます。あなたはまだこの質問をするための●●●●
餌を与えないでください状態です。
①コマンドでphpが動く環境を構築できる
②ローカルでwebからphpが動く環境を構築できる
③phpでprintデバッグできる
④レンタル共有サーバーでwebからphpが動く環境を構築できる
⑤VPSでwebからphpが動く環境を構築できる
⑥コマンドでphpを動かしてローカルCLIデバッグ可能な環境を構築できる
⑦コマンドでphpを動かしてローカルGUIデバッグ可能な環境を構築できる
⑧ローカルでwebからphpを動かしてローカルGUIデバッグ可能な環境を構築できる
⑨リモートでコマンドでphpを動かしてリモートCLIデバッグ可能な環境を構築できる
⑩リモートでコマンドでphpを動かしてリモートGUIデバッグ可能な環境を構築できる
⑪リモートでwebからphpを動かしてリモートGUIデバッグ可能な環境を構築できる
⑫dockerを使ってコマンドでphpを動かす環境を構築できる
⑬dockerを使ってコマンドでphpを動かしてローカルCLIデバッグ可能な環境を構築できる
⑬dockerを使ってコマンドでphpを動かしてホストからCLIデバッグ可能な環境を構築できる
⑭dockerを使ってコマンドでphpを動かしてホストからGUIデバッグ可能な環境を構築できる
⑮dockerを使ってwebからphpを動かしてホストからGUIデバッグ可能な環境を構築できる
(ここに数段あるけど省略)
⑯dockerを使ってwebからphpとLaravelを動かしてホストからGUIデバッグ可能な環境を構築できる
多分せいぜい③くらい。他人に質問して進んでも前進も後退もできない。③に戻る以外の選択肢がない。
1つ1つの段階はそんな段差がなく、人によっては数分で進む。でも他人に聞く人は何年経っても③に止まったまま。
