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:
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:
And on PhpStorm:
PhpStorm CLI Interpreter using docker-compose:
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#
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With