Skip to content
Advertisement

Xdebug silently catches connections with no reason

The application runs on docker containers: nginx and php-fpm. Xdebug is configured with PhpStorm. The app was working correctly until suddenly Xdebug started to catch all connections even when I didn’t enable debugging. I didn’t even change anything in configuration – it just started to do this (a bit magic but of course there should be something).

Why it’s Xdebug: if I remove the Xdebug settings from Dockerfile, everything starts working. Also, requests hang like it happens when I debug them, i.e. they die after a few minutes waiting with the 504 Gateway Time-out error.

PhpStorm doesn’t start a debug session, so it happens silently. Closing PhpStorm doesn’t help. Restart of containers, the docker daemon itself and even OS don’t help as well. Nothing changes in different browsers.

php-fpm/Dockerfile:

FROM php:7.3.18-fpm-alpine

RUN apk add --no-cache $PHPIZE_DEPS 
    && pecl install xdebug-2.9.8 
    && docker-php-ext-enable xdebug

#...there are more lines, but even when I remove them, the issue remains

#When I comment this line and do `docker-compose build && docker-compose down && docker-compose up -d`, 
# the app returns to life.
COPY xdebug.ini /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini

php-fpm/xdebug.ini:

zend_extension=xdebug.so
xdebug.remote_enable=1
xdebug.remote_connect_back=off
xdebug.remote_host=docker.for.mac.localhost
xdebug.remote_port=10000
xdebug.idekey=PHPSTORM
xdebug.remote_autostart=true
xdebug.var_display_max_depth = 16
xdebug.var_display_max_children = 256
xdebug.var_display_max_data = -1

docker-compose.yml:

version: '3.7'
services:
  nginx:
    image: nginx:stable
    volumes:
      - ./docker/nginx/vhost.conf.template:/tmp/vhost.conf.template
      - ./docker/nginx/logs:/logs
      - ./:/app
    depends_on:
      - php-fpm
  php-fpm:
    build: docker/php-fpm
    environment:
      PHP_IDE_CONFIG: serverName=app.local
    volumes:
      - ./:/app

nginx/vhost.conf:

server {
    charset              utf-8;
    client_max_body_size 250M;
    listen               80;

    server_name app.local;
    root        /app/public;
    index       index.php;

    access_log  /logs/nginx.app.access.log;
    error_log   /logs/nginx.app.error.log;

    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }

    location ~ .php$ {
            try_files $uri =404;
            fastcgi_pass fpm:9000;
            include /etc/nginx/fastcgi_params;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $request_filename;
    }

    location ~* "/." {
        deny    all;
        return  404;
    }
}

It’s Docker Desktop 2.5.0 on MacOS 10.14.6.

What could it be?

Advertisement

Answer

Why it’s Xdebug: if I remove the Xdebug settings from Dockerfile, everything starts working. Also, requests hang like it happens when I debug them, i.e. they die after a few minutes waiting with the 504 Gateway Time-out error.

Enable Xdebug log to confirm that the debug session is established and check what communication is going on there (if any). This should give you some clues on what that might be.

Anyway, it looks like you have some service on that TCP 10000 port already (on your host OS (Mac)) that prevents PhpStorm from listening there (IDE can detect already occupied port on Windows and Linux but not on Mac — WI-29443).

Use something like sudo lsof -nP -iTCP -sTCP:LISTEN and check what that service might be. Then either shutdown that app or use another port (either for that app or for your Xdebug communications).

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement