简体   繁体   English

使用 VSCode 和 Docker 调试 PHP

[英]Debug PHP with VSCode and Docker

I'm trying to debug a PHP app running on Docker with VSCode, but without success.我正在尝试使用 VSCode 调试在 Docker 上运行的 PHP 应用程序,但没有成功。

In the past I was able to easily debug my PHP apps with VSCode running WAMP Server, but since I started working with Docker I'm unable to get debug working.过去,我可以使用运行 WAMP Server 的 VSCode 轻松调试我的 PHP 应用程序,但自从我开始使用 Docker 后,我无法进行调试。 Searched for several tutorials online, checked some threads here on StackOverflow (ex.: Docker and XDebug not reading breakpoints VSCode ), but I'm still not able to get this working.在网上搜索了几个教程,在 StackOverflow 上检查了一些线程(例如: Docker and XDebug not reading breakpoints VSCode ),但我仍然无法正常工作。

Dockerfile: 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.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码头工人-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 VSCode:启动.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
    }
]

When I debug the app no breakpoint is being triggered.当我调试应用程序时,没有触发断点。 What am I doing wrong?我究竟做错了什么?

UPDATE: Based on some suggestions i've updated my docker-compose.yml and my launch.json files but nothing changed.更新:根据一些建议,我更新了 docker-compose.yml 和 launch.json 文件,但没有任何改变。

docker-compose.yml码头工人-compose.yml

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

launch.json启动.json

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

VSCode Debug Console: VSCode 调试控制台:

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

UPDATE #2: Removed the Xdebug port (9000) from the docker-compose.yml settings.更新 #2:从 docker-compose.yml 设置中删除了 Xdebug 端口(9000)。 Here is the xdebug log result:这是 xdebug 日志结果:

Log opened at 2018-09-30 22:21:09 I: Connecting to configured address/port: host.docker.internal:9000.日志于 2018-09-30 22:21:09 打开 I:连接到配置的地址/端口:host.docker.internal:9000。 E: Time-out connecting to client (Waited: 200 ms). E:连接到客户端超时(等待:200 毫秒)。 :-( Log closed at 2018-09-30 22:21:09 :-( 日志于 2018-09-30 22:21:09 关闭

Log opened at 2018-09-30 22:21:17 I: Connecting to configured address/port: host.docker.internal:9000.日志于 2018-09-30 22:21:17 打开 I:连接到配置的地址/端口:host.docker.internal:9000。 E: Time-out connecting to client (Waited: 200 ms). E:连接到客户端超时(等待:200 毫秒)。 :-( Log closed at 2018-09-30 22:21:17 :-( 日志于 2018-09-30 22:21:17 关闭

Log opened at 2018-09-30 22:21:18 I: Connecting to configured address/port: host.docker.internal:9000.日志于 2018-09-30 22:21:18 打开 I:连接到配置的地址/端口:host.docker.internal:9000。 E: Time-out connecting to client (Waited: 200 ms). E:连接到客户端超时(等待:200 毫秒)。 :-( Log closed at 2018-09-30 22:21:18 :-( 日志于 2018-09-30 22:21:18 关闭

Log opened at 2018-09-30 22:21:18 I: Connecting to configured address/port: host.docker.internal:9000.日志于 2018-09-30 22:21:18 打开 I:连接到配置的地址/端口:host.docker.internal:9000。 E: Time-out connecting to client (Waited: 200 ms). E:连接到客户端超时(等待:200 毫秒)。 :-( Log closed at 2018-09-30 22:21:18 :-( 日志于 2018-09-30 22:21:18 关闭

Any more suggestions?还有什么建议吗?

Managed to solve my issue with the following settings:设法通过以下设置解决了我的问题:

launch.json启动.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 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

Since xdebug version 3 there are breaking changes in config names.自 xdebug 版本 3 以来,配置名称发生了重大变化。

My current working dockerfile:我当前工作的 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 debug config: VSC 调试配置:

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

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

Well, I have tried all the above answers and have spend hours but got not success because in all the above answer some concepts were not clear to new x-debug user just like me.好吧,我已经尝试了上述所有答案并且花了几个小时但没有成功,因为在上述所有答案中,一些概念对于像我这样的新 x-debug 用户来说并不清楚。 I wish that I would have know the following concepts before starting setting up x-debugger, running on a docker container or a remote server, with my VS Code's PHP Debug.我希望在使用 VS Code 的 PHP Debug 开始设置 x-debugger、在 docker 容器或远程服务器上运行之前,我能够了解以下概念。 I am sure this will help searching for the solution of such issue.我相信这将有助于寻找此类问题的解决方案。

X-Debug Settings X 调试设置

I was not certain that if my machine should be considered as client host or server host, though it was obvious but when you have spent hours on configurations then simple things started to get tricky ones.我不确定我的机器是否应该被视为客户端主机或服务器主机,虽然这很明显,但是当您花费数小时进行配置时,简单的事情开始变得棘手。

xdebug.client_host xdebug.client_host

According to X-debug Docs:根据 X-debug Docs:

This address should be the address of the machine where your IDE or debugging client is listening for incoming debugging connections.此地址应该是the address of the machine where your IDE or debugging client is listening for

I think, with the above piece of text needs no explanation.我想,有了上面这段文字就不用解释了。 Hence, setup it up as follows in our x-debug configurations ie xdebug.ini:因此,在我们的 x-debug 配置中进行如下设置,即 xdebug.ini:

xdebug.client_host=host.docker.internal

xdebug.remote_host xdebug.remote_host

This was a bit tricky config to me.这对我来说有点棘手。 Actually there is no such configurations for x-debug but I kept thinking about it.实际上 x-debug 没有这样的配置,但我一直在想它。 :-) :-)

PHP-Debug Settings PHP-调试设置

hostname主机名

I had been using different configurations except this one (as in this thread no one had mentioned this) :-) and there was no success.除了这个之外,我一直在使用不同的配置(因为在这个线程中没有人提到过这个):-) 并且没有成功。 I then re-explored the docs of PHP Debug came to know that it must get set to localhost in my case, and gave it a try, and boom!然后,我重新浏览了 PHP Debug 的文档,了解到在我的情况下它必须设置为localhost ,并试了一下,然后就成功了! It worked!有效!

"hostname": "localhost"

port港口

The should be set same as the xdebug.client_port and default value is 9300. Repeatedly mentioned in above answers.应设置为与 xdebug.client_port 相同,默认值为 9300。在上面的答案中反复提到。

pathMappings路径映射

This is an other really important config when trying to setup a docker server with your VS Code PHP Debug.在尝试使用 VS Code PHP Debug 设置 docker 服务器时,这是另一个非常重要的配置。 It is an object and must map your local directory structure to the server/container directory ie /home/user/proj/html: ${workspaceRoot}.它是一个对象,必须将您的本地目录结构映射到服务器/容器目录,即 /home/user/proj/html:${workspaceRoot}。 I can't further explain it, it did worked for me and I have mentioned it here.我无法进一步解释它,它确实对我有用,我在这里提到过。 I will welcome if someone explain it.如果有人解释,我会欢迎。

Here are my final settings:这是我的最终设置:

在此处输入图像描述

在此处输入图像描述

Hope this will help and save your hours :-)希望这会有所帮助并节省您的时间:-)

you are missing port :9000 (or :9001 ) in the docker-compose.yml ,您在docker-compose.yml中缺少端口:9000 (或:9001 ),

which needs to be connectable, for the IDE to connect from the outside.它需要是可连接的,以便 IDE 从外部连接。

for VSCode the PHP Debug extension might be required to interact with xdebug .对于 VSCode,可能需要PHP Debug扩展来与xdebug交互。

the default launch.json only uses port: 9000 only once - and has log: true .默认的launch.json只使用一次port: 9000 - 并且有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
    }
  ]
}

also see vscode-php-debug and starting the debugger .另请参阅vscode-php-debug启动调试器

XDEBUG v3 example for Symfony Symfony 的 XDEBUG v3 示例

在此处输入图像描述

docker-compose.yml:码头工人-compose.yml:

###> XDEBUG 3 ###
# Use your client IP here
# Linux: run "ip a | grep docker0"
# Windows (with WSL2): Run "grep nameserver /etc/resolv.conf  | cut -d ' ' -f2"
# MacOS: host.docker.internal
###< XDEBUG 3 ###
environment:
  XDEBUG_CLIENT_HOST: 172.17.0.1
  XDEBUG_CLIENT_PORT: 9003
  PHP_IDE_CONFIG: serverName=Docker
volumes:
  - ./xdebug.ini:/usr/local/etc/php/conf.d/xdebug.ini

xdebug.ini xdebug.ini

xdebug.mode=debug,coverage
xdebug.start_with_request=yes
xdebug.client_host=${XDEBUG_CLIENT_HOST}
xdebug.client_port=${XDEBUG_CLIENT_PORT}

xdebug.ini for php:7.2-fpm-alpine3.8 xdebug.ini for php:7.2-fpm-alpine3.8

zend_extension=xdebug.so

[xdebug]
xdebug.cli_color = 1
xdebug.mode = debug
xdebug.start_with_request = yes
xdebug.discover_client_host = 0
xdebug.client_host=host.docker.internal
xdebug.ideKey=docker

Example config vscode modify pathMappings示例配置 vscode 修改 pathMappings

launch.json:启动.json:

{

    "version": "0.2.0",
    "configurations": [{
        "name": "Listen for XDebug",
        "type": "php",
        "request": "launch",
        "port": 9003,
        "log": true,
        "externalConsole": false,
        "pathMappings": {
            "/appdata/www": "${workspaceRoot}/api",
        },
        "ignore": [
            "**/vendor/**/*.php"
        ]
    },
]
}

For everyone running on the following stack:对于在以下堆栈上运行的每个人:

  • Ubuntu Ubuntu
  • XDebug 3 XDebug 3
  • Laravel Sail Laravel 帆

docker/8.0/php.ini码头工人/8.0/php.ini

 [xdebug] xdebug.discover_client_host=true xdebug.start_with_request=yes

.env .env

 SAIL_XDEBUG_MODE=develop,debug # For linux only: # you may read up about how to get the ip here: https://laravel.com/docs/8.x/sail#debugging-with-xdebug SAIL_XDEBUG_CONFIG="client_host=172.22.0.1"

launch.json启动.json

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

使用 php7,端口更改为 9003

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM