使用 VSCode 和 Docker 调试 PHP

我正在尝试使用VSCode调试在Docker上运行的PHP应用程序,但没有成功。

过去,我能够使用运行WAMP Server的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
    }
]

调试应用时,没有触发断点。我做错了什么?

更新:根据一些建议,我已经更新了我的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 }

更新#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

还有其他建议吗?


答案 1

设法通过以下设置解决了我的问题:

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

答案 2

从 xdebug 版本 3 开始,配置名称发生了重大更改。

我当前工作的 dockerfile:

RUN apt-get update; \
        apt-get -y --no-install-recommends install \
            php7.4-dev \
            php-pear \
            libcurl3-openssl-dev \
            libmcrypt-dev \
            libxml2-dev \
            libpng-dev \
        ; \
        pecl install xdebug; \
        { \
            echo "[xdebug]"; \
            echo "zend_extension=$(find /usr/lib/php/ -name xdebug.so)"; \
            echo "xdebug.mode=debug"; \
            echo "xdebug.start_with_request=yes"; \
            echo "xdebug.client_host=host.docker.internal"; \
            echo "xdebug.client_port=9001"; \
        } >> /etc/php/7.4/mods-available/xdebug.ini; \
        phpenmod -v 7.4 xdebug; \

VSC 调试配置:

    "configurations": [
        {
            "name": "XDebug (Docker)",
            "type": "php",
            "request": "launch",
            "port": 9001,
            "pathMappings": {
                "/var/www/app": "${workspaceRoot}",
            }
        }
    ]

更多信息: https://xdebug.org/docs/upgrade_guide


推荐