web-dev-qa-db-ja.com

デバッグPHP VSCodeおよびDockerを使用

PHP VSCodeを使用してDocker上で実行されているが成功しないアプリをデバッグしようとしています。

以前は、WAMPサーバーを実行しているVSCodeでPHPアプリを簡単にデバッグできましたが、Dockerでの作業を開始して以来、デバッグが機能しません。オンラインでいくつかのチュートリアルを検索し、ここでStackOverflowのいくつかのスレッド(例: DockerとXDebugはブレークポイントVSCodeを読み取っていません )が、私はまだこれを動作させることができません。

Dockerfile:

FROM php:7.1.8-Apache

COPY /cms /srv/app/cms
COPY .docker/cms/vhosts/vhost.conf /etc/Apache2/sites-available/cms.conf
COPY .docker/cms/vhosts/vhost-ssl.conf /etc/Apache2/sites-available/cms-ssl.conf
COPY .docker/cms/vhosts/certificate.conf /etc/ssl/certs/certificate.conf
COPY .docker/cms/xdebug.ini /usr/local/etc/php/conf.d/xdebug.ini

WORKDIR /srv/app/cms

RUN docker-php-ext-install mbstring pdo pdo_mysql
RUN pecl install xdebug 
RUN docker-php-ext-enable xdebug
RUN chown -R www-data:www-data /srv/app/cms
RUN openssl req -x509 -new -out /etc/ssl/certs/ssl-cert-cms.crt -config /etc/ssl/certs/certificate.conf
RUN a2ensite cms.conf
RUN a2ensite cms-ssl.conf
RUN a2enmod rewrite
RUN a2enmod ssl

xdebug.ini

[xdebug]
xdebug.default_enable=1
xdebug.remote_enable=1
xdebug.remote_port=9000
xdebug.remote_connect_back=0
xdebug.remote_Host='Host.docker.internal'
xdebug.idekey='VSCODE'
xdebug.remote_autostart=1

docker-compose.yml

version: '3.7'
services:
cms:
  build:
    context: .
    dockerfile: .docker/cms/Dockerfile
  image: php:7.1.8-Apache
  ports:
    - 18080:80
    - 14430:443
  volumes:
    - ./cms:/srv/app/cms
  links:
    - mysql
    - redis
  environment:
    DB_Host: mysql
    VIRTUAL_Host: my.app.localhost
    PHP_EXTENSION_XDEBUG: 1

VSCode:launch.json

"configurations": [
    {
        "name": "Listen for XDebug",
        "type": "php",
        "request": "launch",
        "pathMappings": {
           "/srv/app/cms": "${workspaceRoot}/my.app/cms",
        },
        "port": 9000
    }, {
        "name": "Launch currently open script",
        "type": "php",
        "request": "launch",
        "program": "${file}",
        "cwd": "${fileDirname}",
        "port": 9000
    }
]

アプリをデバッグしても、ブレークポイントはトリガーされません。私は何を間違えていますか?

UPDATE:いくつかの提案に基づいて、docker-compose.ymlファイルとlaunch.jsonファイルを更新しましたが、何も変更されていません。

docker-compose.yml

ports:
  - 18080:80
  - 14430:443
  - 9000:9000 //added new xdebug default port

launch.json

"configurations": [
    {
        "name": "Listen for XDebug",
        "type": "php",
        "request": "launch",
        "pathMappings": {
           "/srv/app/cms": "${workspaceRoot}/my.app/cms",
        },
        "port": 9000,
        "log": true
    }
]

VSCodeデバッグコンソール:

<- launchResponse
Response {
seq: 0,
type: 'response',
request_seq: 2,
command: 'launch',
success: true }

UPDATE#2:docker-compose.yml設定からXdebugポート(9000)を削除しました。 xdebugログの結果は次のとおりです。

2018-09-30 22:21:09にログが開かれましたI:設定されたアドレス/ポートに接続:Host.docker.internal:9000。 E:クライアントへの接続タイムアウト(待機時間:200ミリ秒)。 :-(ログは2018-09-30 22:21:09に終了しました

2018-09-30 22:21:17にログが開かれましたI:設定されたアドレス/ポートに接続:Host.docker.internal:9000。 E:クライアントへの接続タイムアウト(待機時間:200ミリ秒)。 :-(ログは2018-09-30 22:21:17に終了しました

2018-09-30 22:21:18にログが開かれましたI:設定されたアドレス/ポートに接続します:Host.docker.internal:9000。 E:クライアントへの接続タイムアウト(待機時間:200ミリ秒)。 :-(ログは2018-09-30 22:21:18に終了しました

2018-09-30 22:21:18にログが開かれましたI:設定されたアドレス/ポートに接続します:Host.docker.internal:9000。 E:クライアントへの接続タイムアウト(待機時間:200ミリ秒)。 :-(ログは2018-09-30 22:21:18に終了しました

他に提案はありますか?

更新#3:次の設定を使用して問題を解決しました:

launch.json

{
    "version": "0.2.0",
    "configurations": [{
            "name": "Listen for XDebug",
            "type": "php",
            "request": "launch",
            "port": 9000,
            "log": true,
            "externalConsole": false,
            "pathMappings": {
                "/srv/app/cms": "${workspaceRoot}/cms",
            },
            "ignore": [
                "**/vendor/**/*.php"
            ]
        },
    ]
}

xdebug.ini

zend_extension=/usr/local/lib/php/extensions/no-debug-non-zts-20160303/xdebug.so
xdebug.default_enable=1
xdebug.remote_enable=1
xdebug.remote_port=9000
xdebug.remote_handler=dbgp
xdebug.remote_connect_back=0
xdebug.remote_Host=host.docker.internal
xdebug.idekey=VSCODE
xdebug.remote_autostart=1
xdebug.remote_log=/usr/local/etc/php/xdebug.log
14
Ricky

次の設定で問題を解決できました。

launch.json

{
    "version": "0.2.0",
    "configurations": [{
            "name": "Listen for XDebug",
            "type": "php",
            "request": "launch",
            "port": 9000,
            "log": true,
            "externalConsole": false,
            "pathMappings": {
                "/srv/app/cms": "${workspaceRoot}/cms",
            },
            "ignore": [
                "**/vendor/**/*.php"
            ]
        },
    ]
}

xdebug.ini

zend_extension=/usr/local/lib/php/extensions/no-debug-non-zts-20160303/xdebug.so
xdebug.default_enable=1
xdebug.remote_enable=1
xdebug.remote_port=9000
xdebug.remote_handler=dbgp
xdebug.remote_connect_back=0
xdebug.remote_Host=host.docker.internal
xdebug.idekey=VSCODE
xdebug.remote_autostart=1
xdebug.remote_log=/usr/local/etc/php/xdebug.log
6
Ricky

:9000にポート:9001(またはdocker-compose.yml)がありません。

IDEが外部から接続するために、接続可能である必要があります。

vSCodeの場合、xdebugと対話するには PHP Debug 拡張が必要になる場合があります。

デフォルトのlaunch.jsonport:9000を1回だけ使用し、log:trueを使用します。

{
  "configurations": [{
      "name": "Listen for XDebug",
      "type": "php",
      "request": "launch",
      "port": 9000,
      "log": true
    }, {
      "name": "Launch",
      "request": "launch",
      "type": "php",
      "program": "${file}",
      "cwd": "${workspaceRoot}",
      "externalConsole": false
    }
  ]
}

vscode-php-debug および デバッガの起動 も参照してください。

1
Martin Zeitler