简体   繁体   中英

PHP XDebug on Docker Containers do not works in VS Code (Laravel and docker-compose)

I cannot manage to run php debugger in my docker-compose containers environment using visual studio code and Xdebug.

I try all the steps in this article and various questions like this or this and this

I have this docker-compose.yml file (php conatainer section):

version: '2'
services:
    php:
        build: 
            context: images/php
        ports: 
            - "9000:9000"
        volumes:
            - ./www:/var/www
        links:
            - mysql
        depends_on: 
            - mysql
        network_mode: "bridge"

The ./www volume is my Laravel root application path, mounted to the php container:

在此处输入图片说明

This is the php Dockerfile section where I install Xdebug:

RUN yes | pecl install xdebug \
    && echo "zend_extension=$(find /usr/local/lib/php/extensions/ -name xdebug.so)" > /usr/local/etc/php/conf.d/xdebug.ini \
    && echo 'xdebug.default_enable=1' >> /usr/local/etc/php/conf.d/xdebug.ini \
    && echo 'xdebug.remote_enable=1' >> /usr/local/etc/php/conf.d/xdebug.ini \
    && echo 'xdebug.remote_connect_back=1' >> /usr/local/etc/php/conf.d/xdebug.ini \
    && echo 'xdebug.remote_autostart=1' >> /usr/local/etc/php/conf.d/xdebug.ini \
    && echo 'xdebug.remote_handler="dbgp"' >> /usr/local/etc/php/conf.d/xdebug.ini \
    && echo 'xdebug.remote_port=9000' >> /usr/local/etc/php/conf.d/xdebug.ini \
    && echo 'xdebug.remote_host=0.0.0.0' >> /usr/local/etc/php/conf.d/xdebug.ini \
    && echo 'xdebug.max_nesting_level=250' >> /usr/local/etc/php/conf.d/xdebug.ini \
    && echo 'xdebug.remote_log=/var/www/xdebug.log' >> /usr/local/etc/php/conf.d/xdebug.ini \

When I build my containers I have no errors, and attaching a shell to php container and running php -v I have this output:

PHP 7.2.3 (cli) (built: Mar 22 2018 22:03:09) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
    with Xdebug v2.6.1, Copyright (c) 2002-2018, by Derick Rethans

So, Xdebug is intalled into the container. I installed PHP-debug extension and set this VSCode debug configuration:

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

The app is working, I set a breakpoint to www/public/index.php at this line

$app = require_once __DIR__.'/../bootstrap/app.php';

and start to debug. I have a debug console output:

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

When I refresh the app in my browser I have two new logs in my laravel root into the file ./www/xdebug.log :

Log opened at 2019-01-13 13:29:35
I: Checking remote connect back address.
I: Checking header 'HTTP_X_FORWARDED_FOR'.
I: Checking header 'REMOTE_ADDR'.
I: Remote address found, connecting to 172.17.0.1:9000.
I: Connected to client. :-)
-> <init xmlns="urn:debugger_protocol_v1" xmlns:xdebug="http://xdebug.org/dbgp/xdebug" fileuri="file:///var/www/public/index.php" language="PHP" xdebug:language_version="7.2.3" protocol_version="1.0" appid="6" idekey="user"><engine version="2.6.1"><![CDATA[Xdebug]]></engine><author><![CDATA[Derick Rethans]]></author><url><![CDATA[http://xdebug.org]]></url><copyright><![CDATA[Copyright (c) 2002-2018 by Derick Rethans]]></copyright></init>

-> <response xmlns="urn:debugger_protocol_v1" xmlns:xdebug="http://xdebug.org/dbgp/xdebug" status="stopping" reason="ok"></response>

Log closed at 2019-01-13 13:29:36

Log opened at 2019-01-13 13:51:23
I: Checking remote connect back address.
I: Checking header 'HTTP_X_FORWARDED_FOR'.
I: Checking header 'REMOTE_ADDR'.
I: Remote address found, connecting to 172.17.0.1:9000.
I: Connected to client. :-)
-> <init xmlns="urn:debugger_protocol_v1" xmlns:xdebug="http://xdebug.org/dbgp/xdebug" fileuri="file:///var/www/public/index.php" language="PHP" xdebug:language_version="7.2.3" protocol_version="1.0" appid="7" idekey="user"><engine version="2.6.1"><![CDATA[Xdebug]]></engine><author><![CDATA[Derick Rethans]]></author><url><![CDATA[http://xdebug.org]]></url><copyright><![CDATA[Copyright (c) 2002-2018 by Derick Rethans]]></copyright></init>

-> <response xmlns="urn:debugger_protocol_v1" xmlns:xdebug="http://xdebug.org/dbgp/xdebug" status="stopping" reason="ok"></response>

Log closed at 2019-01-13 13:51:24

But the debugger do not stops to my breakpoint. I tried changing xdebug parameters and vscode configuration many times, but I cannot solve the problem.

Please help me, I've run out of ideas...

Just figured this out myself and thought I would post it in case it helps solve problem for others

As far as I understand the remote_host should be set to 'host.docker.internal'

I've set this in my docker-compose file:

version: '3'
services:
  phpapp:
    image: phpapp
    container_name: phpapp
    environment: 
      XDEBUG_CONFIG: remote_host=host.docker.internal remote_port=9050 remote_enable=1 remote_autostart=1
    ports:
      - '8080:80'
    volumes:
      - 'c:/dev/www:/var/www/html'

Since xdebug version 3 there are breaking changes in config names.

Change dockerfile:

RUN yes | pecl install xdebug \
    && echo "zend_extension=$(find /usr/local/lib/php/extensions/ -name xdebug.so)" > /usr/local/etc/php/conf.d/xdebug.ini \
    && echo 'xdebug.mode=debug' >> /usr/local/etc/php/conf.d/xdebug.ini \
    && echo 'xdebug.start_with_request=yes' >> /usr/local/etc/php/conf.d/xdebug.ini \
    && echo 'xdebug.client_host=host.docker.internal' >> /usr/local/etc/php/conf.d/xdebug.ini \
    && echo 'xdebug.client_port=9050' >> /usr/local/etc/php/conf.d/xdebug.ini \

Change port also in vsc config "port": 9050,

btw you don't need to expose 9000 or 9050 port

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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