IDE debugging with Docker, nginx, PHP7-FPM and Xdebug

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



IDE debugging with Docker, nginx, PHP7-FPM and Xdebug



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.


docker-compose



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



Here's my settings:



My .env file:


.env


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:


Dockerfile


FROM php:7.2-fpm

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



The docker-compose.yml file:


docker-compose.yml


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:


php-dev.ini


; 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:


default.conf


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.


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


ports


expose


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:


nc


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#





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? 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 at 20:54






@MikeSchinkel I added an image with information about the interpreter.
– Thiago Pereira
Aug 11 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 at 0:18


Port 9009 is busy





@LazyOne Tks. So I updated the question with your suggestions
– Thiago Pereira
Aug 12 at 1: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 at 10:41


sudo lsof -nP -iTCP -sTCP:LISTEN


netstat


xdebug.remote_connect_back = 1


remote_host




1 Answer
1



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


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


xdebug.remote_connect_back = 1



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


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.


remote_connect_back






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard