4

I'm currently preparing a development stack on macOS with docker-compose to be able to use Xdebug (Port: 9009) on PHP7-FPM (Port: 9000) and nginx (Port: 80) server.

Apparently the configuration is OK, but I am not able to debug through the IDE.

Here's my settings:

My .env file:

APP_NAME=testeXdebug
HOST_SERVER_NAME=myapp
HOST_IP=docker.for.mac.localhost
# Use docker.for.mac.localhost - for OS X
# Use docker.for.win.localhost - for Windows

The Dockerfile PHP7-FPM + Xdebug:

FROM php:7.2-fpm

RUN pecl install xdebug \
    && docker-php-ext-enable xdebug

The docker-compose.yml file:

version: '3.5'

services:
  web:
    image: nginx:1.15.2
    ports:
      - '80:80'
    volumes:
      - '.:/usr/share/nginx/html'
      - './config/default.conf:/etc/nginx/conf.d/default.conf'
      - '/tmp/${APP_NAME}/web:/var/log/nginx'
    env_file:
      - '.env'
    depends_on:
      - 'php-fpm'
    links:
      - 'php-fpm'

  php-fpm:
    build: './docker'
    ports:
      - '9000:9000'
      - '9009:9009'
    expose:
      - 9000
      - 9009
    volumes:
      - '.:/usr/share/nginx/html'
      - './config/php-dev.ini:/usr/local/etc/php/conf.d/php-dev.ini'
      - '/tmp/${APP_NAME}/php-fpm:/var/log/xdebug'
    environment:
      XDEBUG_CONFIG: "remote_host=${HOST_IP}"
      PHP_IDE_CONFIG: "serverName=${HOST_SERVER_NAME}"
    env_file:
      - '.env'

The php-dev.ini file:

; Xdebug
xdebug.default_enable = 1
xdebug.remote_enable = 1
xdebug.remote_autostart = 1
xdebug.remote_connect_back = 1
xdebug.remote_port = 9009
xdebug.profiler_enable = 0
xdebug.idekey = PHPSTORM
xdebug.remote_handler = dbgp
xdebug.remote_mode = req
xdebug.remote_log = /var/log/xdebug/xdebug.log

The nginx default.conf file:

server {
    listen          80;
    server_name     myapp;
    root            /usr/share/nginx/html;
    index           index.php index.html index.htm;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        fastcgi_pass    php-fpm:9000;
        fastcgi_index   index.php;
        fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include         fastcgi_params;
    }

    location ~ /\.ht {
        deny  all;
    }
}

Then when accessing the server through the browser with Xdebug helper extension active:

enter image description here

I get this debug log (xdebug):

Log opened at 2018-08-11 19:22:53
I: Checking remote connect back address.
I: Checking header 'HTTP_X_FORWARDED_FOR'.
I: Checking header 'REMOTE_ADDR'.
I: Remote address found, connecting to 192.168.128.1:9009.
I: Connected to client. :-)
-> <init xmlns="urn:debugger_protocol_v1" xmlns:xdebug="http://xdebug.org/dbgp/xdebug" fileuri="file:///usr/share/nginx/html/index.php" language="PHP" xdebug:language_version="7.2.8" protocol_version="1.0" appid="9" idekey="PHPSTORM"><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 2018-08-11 19:22:53

nginx access log:

192.168.128.1 - - [11/Aug/2018:18:57:25 +0000] "GET /favicon.ico HTTP/1.1" 200 94197 "http://docker.for.mac.localhost/index.php?XDEBUG_SESSION_START=PHPSTORM" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36" "-"
192.168.128.1 - - [11/Aug/2018:19:22:53 +0000] "GET /index.php?XDEBUG_SESSION_START=PHPSTORM HTTP/1.1" 200 94341 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36" "-"
192.168.128.1 - - [11/Aug/2018:19:22:53 +0000] "GET /favicon.ico HTTP/1.1" 200 94205 "http://docker.for.mac.localhost/index.php?XDEBUG_SESSION_START=PHPSTORM" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36" "-"
192.168.128.1 - - [11/Aug/2018:19:30:12 +0000] "GET /_intellij_phpdebug_validator.php HTTP/1.1" 200 516 "-" "Java/1.8.0_152-release" "-"

And setup VSCode with debug extension with this:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Listen for XDebug",
            "type": "php",
            "request": "launch",
            "port": 9009,
            "pathMappings": {
                "/usr/share/nginx/html": "${workspaceRoot}"
            }
        }
    ]
}

Add some break points:

enter image description here

And on PhpStorm:

enter image description here

PhpStorm CLI Interpreter using docker-compose:

enter image description here

But at Start listening for PHP Debug connection I get Port 9009 is busy.

And IDE never start the debug tool... =(
What could I be missing out on? Please help me!


After some comments:

When removing ports and expose settings of php-fpm service from docker-compose.yml

thiago@MA-TPR testeXdebug $ docker ps
CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS              PORTS                NAMES
735fc48fad63        nginx:1.15.2          "nginx -g 'daemon of…"   4 minutes ago       Up 4 minutes        0.0.0.0:80->80/tcp   testexdebug_web_1
b9b16af98fb5        testexdebug_php-fpm   "docker-php-entrypoi…"   4 minutes ago       Up 4 minutes        9000/tcp             testexdebug_php-fpm_1
thiago@MA-TPR testeXdebug $ 

And get this log from xdebug:

Log opened at 2018-08-12 00:56:39
I: Checking remote connect back address.
I: Checking header 'HTTP_X_FORWARDED_FOR'.
I: Checking header 'REMOTE_ADDR'.
I: Remote address found, connecting to 192.168.160.1:9009.
W: Creating socket for '192.168.160.1:9009', poll success, but error: Operation now in progress (29).
E: Could not connect to client. :-(
Log closed at 2018-08-12 00:56:39

Executing nc from PHP container:

root@b9b16af98fb5:/var/www/html# nc -zv docker.for.mac.localhost 9009
Warning: inverse host lookup failed for 192.168.65.2: Unknown host
docker.for.mac.localhost [192.168.65.2] 9009 (?) open
root@b9b16af98fb5:/var/www/html# 
Thiago Pereira
  • 594
  • 13
  • 25
  • Getting XDEBUG working is like trying to get 10 plates to spin on sticks, all at once. One problem might be having is that PhpStorm can't see your PHP.ini file, as it reports. Have you configured PHP in the Languages & Frameworks section? https://www.evernote.com/l/AAVXKjxUZRNB8L-XhXCJg3j3s63jZpJLw2g Also, is your sole purpose for building a Docker to be able to XDEBUG and do local development, or are use interested in working with Docker for other reasons? IOW, what about other alternatives that already work with XDEBUG? – MikeSchinkel Aug 11 '18 at 20:54
  • @MikeSchinkel I added an image with information about the interpreter. – Thiago Pereira Aug 11 '18 at 23:52
  • *"But at Start listening for PHP Debug connection I get `Port 9009 is busy`"* Of course it's busy -- you are using it. You have exposed 9009 in Docker? Why? What for? If you expose it .. Docker listens on that port and forwards any connections into container. But .. it's IDE/VSCode/PhpStorm that must be listening it.... because **it's Xdebug that connects to IDE and NOT other way around** (RTFM please). So fix that thing first and try again. – LazyOne Aug 12 '18 at 00:18
  • @LazyOne Tks. So I updated the question with your suggestions – Thiago Pereira Aug 12 '18 at 01:19
  • 1) What app listens on 9009? Use `sudo lsof -nP -iTCP -sTCP:LISTEN` on Mac and `netstat` on Windows. 2) `xdebug.remote_connect_back = 1` -- I would better turn this off and specify host in `remote_host` -- try that way. The IP address that Xdebug or nc tries to connect is different every time -- AFAIK it should not be like that. – LazyOne Aug 12 '18 at 10:41
  • @LazyOne it works disable connect back. Thks! – Thiago Pereira Aug 12 '18 at 11:32

2 Answers2

7

But at Start listening for PHP Debug connection I get Port 9009 is busy

There is no need to expose Xdebug port in your Docker container, no need at all.

If you expose it .. Docker will be the one that listens on that port and forwards any connections into container. But it's IDE/VSCode/PhpStorm that must be listening it... because it's Xdebug that connects to IDE and NOT other way around.

Fix that first.

xdebug.remote_connect_back = 1

I recommend turning this off and specify actual host in xdebug.remote_host (docker.for.mac.localhost).

The IP detected by Xdebug with remote_connect_back option (depends on settings I guess, and the way how Docker works) very likely will not be the IP of the host machine. But that's what you need -- that's where your IDE (PhpStorm) / editor (VSCode) runs and Xdebug must be connecting to.

LazyOne
  • 158,824
  • 45
  • 388
  • 391
  • 1
    there is now a general setting for mac and windows and linux I think 'host.docker.internal' but without your second part of response, it did not work and it was not very emphasized so I lost it in reading, but `xdebug.remote_connect_back=0` specifically is also very important!!! I just read your settings, not the text around, so I thought it had to be set to 1 but it has to be zero. Regards various guys playing with ports mappings and expose as you correctly said is useless as xdebug internally in php uses random hiddent port, 9000 concrete port is the remote one. Otherwise thanks for help. – FantomX1 Feb 12 '20 at 18:51
  • so xdebug.remote_connect_back = 0 is also necessary on windows, though on linux I had experience that it was not needed to disable it. – FantomX1 Feb 12 '20 at 19:11
1

If you are on "Docker for Mac", then in you xdebug config file you can also use this:

xdebug.remote_host = host.docker.internal

I WANT TO CONNECT FROM A CONTAINER TO A SERVICE ON THE HOST
The host has a changing IP address (or none if you have no network access). From 18.03 onwards our recommendation is to connect to the special DNS name host.docker.internal, which resolves to the internal IP address used by the host. This is for development purpose and will not work in a production environment outside of Docker for Mac.

The gateway is also reachable as gateway.docker.internal.

hserge
  • 905
  • 1
  • 9
  • 12