Home > Blockchain >  Xdebug 3 in Docker container: Could not connect to debugging client. - Address not available
Xdebug 3 in Docker container: Could not connect to debugging client. - Address not available

Time:07-26

In my Docker container xdebug say: Could not connect to debugging client.

Here is my dockerfile:

FROM php:8.1-fpm-alpine

WORKDIR /var/www/html

RUN apk add --no-cache --repository http://dl-cdn.alpinelinux.org/alpine/edge/community/ --allow-untrusted gnu-libiconv
ENV LD_PRELOAD /usr/lib/preloadable_libiconv.so php

ENV PHP_MEMORY_LIMIT=1G
ENV PHP_UPLOAD_MAX_FILESIZE: 512M
ENV PHP_POST_MAX_SIZE: 512M

RUN docker-php-ext-install pdo

RUN apk add --no-cache libpng libpng-dev && docker-php-ext-install gd && apk del libpng-dev

RUN apk update \
    && apk upgrade \
    && apk add --no-cache \
        freetype \
        libpng \
        libjpeg-turbo \
        freetype-dev \
        libpng-dev \
        jpeg-dev \
        libwebp-dev \
        libjpeg \
        libjpeg-turbo-dev

RUN docker-php-ext-configure gd \
        --with-freetype=/usr/lib/ \
        --with-jpeg=/usr/lib/ \
        --with-webp=/usr

RUN NUMPROC=$(grep -c ^processor /proc/cpuinfo 2>/dev/null || 1) \
    && docker-php-ext-install -j${NUMPROC} gd


RUN apk add --no-cache sqlite-libs
RUN apk add --no-cache icu sqlite git openssh zip
RUN apk add --no-cache --virtual .build-deps icu-dev libxml2-dev sqlite-dev curl-dev
RUN docker-php-ext-install \
        bcmath \
        curl \
        ctype \
        intl \
        pdo \
        pdo_sqlite \
        xml
RUN apk del .build-deps

RUN docker-php-ext-enable pdo_sqlite

# Add xdebug
RUN apk add --no-cache --virtual .build-deps $PHPIZE_DEPS
RUN apk add --update linux-headers
RUN pecl install xdebug-3.1.5
RUN docker-php-ext-enable xdebug
RUN apk del -f .build-deps

# Configure Xdebug
RUN echo "xdebug.start_with_request=yes" >> /usr/local/etc/php/conf.d/xdebug.ini \
    && echo "xdebug.mode=debug" >> /usr/local/etc/php/conf.d/xdebug.ini \
    && echo "xdebug.log=/var/www/html/xdebug/xdebug.log" >> /usr/local/etc/php/conf.d/xdebug.ini \
    && echo "xdebug.discover_client_host=1" >> /usr/local/etc/php/conf.d/xdebug.ini

In the xdebug.log file I see this:

[1] [Step Debug] INFO: Checking remote connect back address.
[1] [Step Debug] INFO: Checking header 'HTTP_X_FORWARDED_FOR'.
[1] [Step Debug] INFO: Checking header 'REMOTE_ADDR'.
[1] [Step Debug] WARN: Could not discover client host through HTTP headers, connecting to configured address/port: localhost:9003. :-|
[1] [Step Debug] WARN: Creating socket for 'localhost:9003', poll success, but error: Operation in progress (29).
[1] [Step Debug] WARN: Creating socket for 'localhost:9003', connect: Address not available.
[1] [Step Debug] ERR: Could not connect to debugging client. Tried: localhost:9003 (fallback through xdebug.client_host/xdebug.client_port) :-(

I tried to get some informations about network, and netstat say this (container build log):

Step 25/28 : RUN /bin/netstat -a -n
 ---> Running in 2cf0502655f5
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags       Type       State         I-Node   Path

So netstat say there isn't any active connections.

I tried to set xdebug.client_host=host.docker.internal, and after this I get this result in log:

[1] [Step Debug] INFO: Connecting to configured address/port: host.docker.internal:9003.
[1] [Step Debug] WARN: Creating socket for 'host.docker.internal:9003', getaddrinfo: Resource temporarily unavailable.
[1] [Step Debug] ERR: Could not connect to debugging client. Tried: host.docker.internal:9003 (through xdebug.client_host/xdebug.client_port) :-(

localhost or 127.0.0.1 also not work as client_host.

I want to use Xdebug only for generating coverage report.

How can solve this issue? What should I set as client_host?

CodePudding user response:

You have currently set Xdebug's mode to xdebug.mode=debug, which is meant for single stepping, and indeed requires connecting to an IDE. If it can't, it will show you this error.

As you are only wanting to use Xdebug for Code Coverage information, then there is no need for Xdebug to connect to anything.

Instead you should change the setting in your Dockerfile to use echo "xdebug.mode=debug" instead. You can also safely remove the line containing discover_client_host.

Xdebug's modes are described in detail the documentation, as well as in this YouTube video.

  • Related