Getting xdebug to work with my container

I’ve struggled to get a dev environment setup and running with a persistent database and with xdebug installed. I finally managed to get my container going with a persistent database and I did finally manage to get xdebug installed with my container. But I am still not getting xdebug to work with vs code and my php_info() line is still showing that although xdebug is installed there are two error present. Any help would be greatly appreciated as I am stuck and can not move forward in my courses. Thanks.

Here are the two erros, google this issue hasn’t helped me.

|[Step Debug] Creating socket for 'host.docker.internal:9003', getaddrinfo: No such file or directory.|[⊕](https://xdebug.org/docs/errors#DBG-W-SOCK1)|
&
[Step Debug] Could not connect to debugging client. Tried: host.docker.internal:9003 (through xdebug.client_host/xdebug.client_port).

This is at the bottom of my output:

<small>/var/www/html/index.php:5:</small><small>string</small> '/usr/local/etc/php/php.ini' *(length=26)*

<small>/var/www/html/index.php:5:</small><small>string</small> '/usr/local/etc/php/conf.d/99-xdebug.ini, /usr/local/etc/php/conf.d/docker-php-ext-gd.ini, /usr/local/etc/php/conf.d/docker-php-ext-pdo_mysql.ini, /usr/local/etc/php/conf.d/docker-php-ext-sodium.ini, /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini, /usr/local/etc/php/conf.d/error_reporting.ini ' *(length=298)*

zend_extension=/usr/local/lib/php/extensions/no-debug-non-zts-20220829/xdebug.so
docker-compose.yml:
version: '3.1'

services:
  php:
    build:
      context: .
      dockerfile: Dockerfile
    volumes:
      ["./:/var/www/html",
      "./docker/php/conf.d/xdebug.ini:/usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini",
      "./docker/php/conf.d/xdebug.ini:/usr/local/etc/php/conf.d/99-xdebug.ini",
      "./docker/php/conf.d/error_reporting.ini:/usr/local/etc/php/conf.d/error_reporting.ini",
      "./docker/php/conf.d/php.ini:/usr/local/etc/php/php.ini"]
    ports:
      ["8081:80"]
    stdin_open: true
    tty: true
  db:
    image: mariadb:10.6
    restart: always
    volumes:
      ["./mariadb/data:/var/lib/mysql"]
    environment:
      MYSQL_ROOT_PASSWORD: notSecureChangeMe
      MYSQL_USER: product_db_user
      MYSQL_PASSWORD: secret
  phpmyadmin:
    image: phpmyadmin
    restart: always
    ports:
      - 8001:80
    environment:
      - PMA_ARBITRARY=1

Dockerfile:

FROM php:8.2-apache
COPY . /var/www/html
COPY ./httpd-vhost.conf /etc/apache/sites_available/
WORKDIR /var/www/html
RUN apt-get update && apt-get install -y \
        libfreetype6-dev \
        libjpeg62-turbo-dev \
        libpng-dev \
    && docker-php-ext-configure gd --with-freetype --with-jpeg \
    && docker-php-ext-install -j$(nproc) gd \
    && docker-php-ext-install pdo_mysql \
    && a2enmod rewrite \
    && pecl install xdebug && docker-php-ext-enable xdebug \
    && echo "zend_extension=$(find /usr/local/lib/php/extensions/ -name xdebug.so)" > /usr/local/etc/php/conf.d/99-xdebug.ini \
    && echo "xdebug.remote_enable=on" >> /usr/local/etc/php/conf.d/99-xdebug.ini \
    && echo "xdebug.remote_autostart=off" >> /usr/local/etc/php/conf.d/99-xdebug.ini
    # \
    # && sed -i '/LoadModule rewrite_module/s/^#//g' /usr/local/apache2/conf/httpd.conf \
    # && sed -i 's#AllowOverride [Nn]one#AllowOverride All#' /usr/local/apache2/conf/httpd.conf

docker/php/conf.d/docker-php-ext-xdebug.ini:

zend_extension=xdebug

zend_extension=/usr/local/lib/php/extensions/no-debug-non-zts-20220829/xdebug.so

[xdebug]

xdebug.mode=develop,debug

xdebug.client_host=host.docker.internal

xdebug.start_with_request=yes

xdebug.log=/tmp/xdebug.log


docker/php/conf.d/php.ini:
zend_extension=/usr/local/lib/php/extensions/no-debug-non-zts-20220829/xdebug.so


docker/php/conf.dxdebug.ini:
zend_extension=xdebug

zend_extension=/usr/local/lib/php/extensions/no-debug-non-zts-20220829/xdebug.so

[xdebug]

xdebug.mode=develop,debug

xdebug.client_host=host.docker.internal

xdebug.start_with_request=yes

xdebug.log=/tmp/xdebug.log

index.php:

<?php

phpinfo();

var_dump(php_ini_loaded_file(), php_ini_scanned_files());

xdebug_info();

$fileContent = file_get_contents('/usr/local/etc/php/php.ini', true);

echo $fileContent . "<br><br>";

$hostFile = file_get_contents('/etc/hosts', true);

echo $hostFile;

update:

I edited your post. Next time, please, do what I described below. Thank you.


Please, format your post according to the following guide: How to format your forum posts
In short: please, use </> button to share codes, terminal outputs, error messages or anything that can contain special characters which would be interpreted by the MarkDown filter. Use the preview feature to make sure your text is formatted as you would expect it and check your post after you have sent it so you can still fix it.

Example code block:

```
echo "I am a code."
echo "An athletic one, and I wanna run."
```

Included the same configuration file twice via bind mount. Then one file changed. Doesn’t this just overwrite the settings from the other file? What is that good for?

I would also recommend sorting all ini files. For example like:
000-php-dev.ini
010-error-reporting.ini
999-xdebug.ini

The error message contains a link to a more detailed description:

Occurs when Xdebug is trying to connect to a debuging client to start a debugging session.
Indicates that Xdebug had an issue obtaining information about the configured hostname or IP address from the Operating System. This can indicate an issue with an entry in the /etc/hosts file or an issue with reaching a DNS server.

host.docker.internal resolves the IP address to the host.
This is intended for development purposes and only works with Docker Desktop for Windows.

If you are using Docker Desktop for Windows and these messages occur, it is also possible that the IDE is not listening to the debugger connection. You must activate this in the IDE. It is also possible that the IDE is listening on the wrong port.

To find the error, you should check that the address host.docker.internal can be resolved in the container and that the IDE is listening on port 9003.

Without trying to find what the problem is in your specific case, I share one of my projects in which I made scripts to support unit testing multiple PHP versions in a project and also support using it from PHPStorm and showing the result on the GUI.

And the documentation about testing

https://iputil.readthedocs.io/en/stable/testing.html

I had to make it work on macos (Docker Desktop) and Linux (without Docker desktop) and the scripts also deal with the different xdebug requirements of different PHP versions.

I should probably have mentioned in at the start if I didn’t but I’m on Linux, and I don’t have Docker Desktop.

Included the same configuration file twice via bind mount. Then one file changed. Doesn’t this just overwrite the settings from the other file? What is that good for?
I really don’t know, I’ve been following web and youtube tutorials but all have been unsuccessful. It’s possible that one of those two lines is from one course and the other line from the other course, but unlikely, I probably got it from a single tutorial that I was following. Would anything get overwritten? I see the same file is being applied twice but to two different xdebug configurations/options.

The error message contains a link to a more detailed description:

Occurs when Xdebug is trying to connect to a debuging client to start a debugging session.
Indicates that Xdebug had an issue obtaining information about the configured hostname or IP address from the Operating System. This can indicate an issue with an entry in the /etc/hosts file or an issue with reaching a DNS server.
Not sure what to do here, I don’t see anything wrong with my /etc/hosts file:

127.0.0.1	localhost
127.0.1.1	garrett-IdeaPad-3...
127.0.0.1   laravel.test
127.0.0.1   phpmvc.localhost

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

Since I’m using Linux, I would I do in place of xdebug.client_host=host.docker.internal ? I do remember seeing something about that only being for Linux once or twice but I also kept seeing on the tutorials I was following and trying the alternative I found didn’t change anything.

UPDATE
I changed host.docker.internal to 127.0.0.1 and now these errors are on my phpinfo page, I think these are new errors

[Step Debug] Creating socket for '127.0.0.1:9003', poll success, but error: Operation now in progress (29).
[Step Debug] Could not connect to debugging client. Tried: 127.0.0.1:9003 (through xdebug.client_host/xdebug.client_port).

In PHP, configurations are primarily set in the php.ini file. However, these settings can be overridden by other .ini files located in the conf.d directory. The conf.d directory can contain multiple .ini files, allowing for granular control over different aspects of PHP’s configuration. When PHP is initialized, it first loads the php.ini file and then any additional .ini files found in the conf.d directory in alphabetical order. If there are any conflicting or identical settings, the ones in the last loaded .ini file from the conf.d directory take precedence.

In the context of networking, 127.0.0.1 is the loopback address, also known as localhost, which refers to the current device used to run the application. When you’re working with Docker, this concept becomes a bit nuanced.

Inside a Docker container, 127.0.0.1 refers to the container itself, not the host machine. This is because each Docker container has its own network namespace, meaning it has its own IP address space and can have its own loopback address. So, when you use 127.0.0.1 inside a Docker container, you’re referring to the container.

On the host machine, 127.0.0.1 or localhost will point to the host itself. This is standard behavior across most systems, where 127.0.0.1 or localhost is used to refer to the network services that are running on the host itself.

In summary, 127.0.0.1 or localhost inside a Docker container will point to that container, and 127.0.0.1 or localhost on the host machine will point to the host itself.

But the port and IP of the host on which Docker and the IDE were started must be accessible from the container with xdebug.

Hope this helps:

Update
I got both of these errors to dissapear from my xdebug_info() output:

[Step Debug] Creating socket for ‘host.docker.internal:9003’, getaddrinfo: No such file or directory.
[Step Debug] Could not connect to debugging client. Tried: host.docker.internal:9003 (through xdebug.client_host/xdebug.client_port).

But vscode is still not stopping on any breakpoints at all. Any suggestions/ideas?

How did you solve it? What did you change eventually? And what are your current XDebug settings?

As of right now I’m able to click Play/Xdebug button (Launch currently open script) at the top left of vs code and it will execute the script and stop at the break points and allow me to walk through and inspect, but it will still not respond or stop at the breakpoints when I load or refresh the page in my browser, and this is after I click the Play/Xdebug button (Listen for xdebug). Note that if this did work I would prefer it to always be listening, the way php storm does at my current dev job. Below is my newest launch.json file:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Listen for Xdebug",
            "type": "node",
            "request": "attach",
            "localRoot": "../",
            "remoteRoot": "/var/www/html",
            "port": 9003
        },
        {
            "name": "Launch currently open script",
            "type": "php",
            "request": "launch",
            "program": "${file}",
            "cwd": "${fileDirname}",
            "port": 9003,
            "runtimeArgs": [
                "-dxdebug.start_with_request=yes"
            ],
            "env": {
                "XDEBUG_MODE": "debug",
                "XDEBUG_CONFIG": "client_port=${port}"
            }
        },
        {
            "name": "Launch Built-in web server",
            "type": "php",
            "request": "launch",
            "runtimeArgs": [
                "-dxdebug.mode=debug",
                "-dxdebug.start_with_request=yes",
                "-S",
                "localhost:80"
            ],
            "program": "",
            "cwd": "${workspaceRoot}",
            "port": 9003,
            "serverReadyAction": {
                "pattern": "Development Server \\(http://localhost:([0-9]+)\\) started",
                "uriFormat": "http://localhost:%s",
                "action": "openExternally"
            }
        }
    ]
}

There’s the reply above, and here’s a better worded and more detailed question that I have on discord right now (that no one is replying to), this might give a better idea of my setup.

I’ve been working for weeks now, probably over a month to get vs code to stop on break points, but everytime I launch the web page it loads and vs code’s debugger never stops. I’m doind this in a container and it took several weeks to get xdebug installing right (I only get to work on this stuff on the weekends) and several more weeks to work out two remaining xdebug erros that were appering when I would run echo xdebug_info(); in my php script. It’s installed, and I’m no longer getting any errors, but I’m still completely stuck as vs code is not stopping when I load the page in the browser. Any help would be greatly appreciated as I have been unable to move forward with my php Udemy courses for over a month now. Thanks.

A file is attached showing my directory structure and some of my files. Discord wouldn’t let me past it here.

// direcotry structure

Garrett@ideaPad-3:newTest3$ ls -lR
.:
total 24
drwxrwxr-x 3 garrett garrett 4096 Mar 17 09:01 docker
-rw-rw-r-- 1 garrett garrett  796 Mar 24 09:32 docker-compose.yml
-rw-rw-r-- 1 garrett garrett  674 Mar 17 10:22 Dockerfile
-rw-rw-r-- 1 garrett garrett  238 Mar 17 09:01 httpd-vhost.conf
-rw-rw-r-- 1 garrett garrett   61 Mar 17 11:10 index.php
drwxrwxr-x 3 garrett garrett 4096 Mar 17 09:01 mariadb

./docker:
total 4
drwxrwxr-x 3 garrett garrett 4096 Mar 17 09:02 php

./docker/php:
total 4
drwxrwxr-x 2 garrett garrett 4096 Mar 17 09:01 conf.d

./docker/php/conf.d:
total 16
-rwxr-xr-x 1 garrett garrett 241 Mar 17 11:02 docker-php-ext-xdebug.ini
-rw-rw-r-- 1 garrett garrett  21 Mar 17 09:01 error_reporting.ini
-rw-rw-r-- 1 garrett garrett  91 Mar 17 10:37 php.ini
-rw-rw-r-- 1 garrett garrett 228 Mar 17 11:47 xdebug.ini

Note that docker-php-ext-xdebug.ini & xdebug.ini are identical.

// index.php
<?php

phpinfo();
echo "hello world";
echo xdebug_info();

?>

// docker-php-ext-xdebug.ini
zend_extension=xdebug
zend_extension=/usr/local/lib/php/extensions/no-debug-non-zts-20220829/xdebug.so

[xdebug]
xdebug.mode=debug
xdebug.client_host=127.0.0.53
xdebug.start_with_request=yes
xdebug.log=/tmp/xdebug.log
xdebug.remote_port=9003

// launch.json			(note that I have tried ./ & ../ for localRoot, ../ seemed correct for my directory structure)
{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Listen for Xdebug",
            "type": "node",
            "request": "attach",
            "localRoot": "../",
            "remoteRoot": "/var/www/html",
            "port": 9003
        },
        {
            "name": "Launch currently open script",
            "type": "php",
            "request": "launch",
            "program": "${file}",
            "cwd": "${fileDirname}",
            "port": 0,
            "runtimeArgs": [
                "-dxdebug.start_with_request=yes"
            ],
            "env": {
                "XDEBUG_MODE": "debug",
                "XDEBUG_CONFIG": "client_port=${port}"
            }
        },
        {
            "name": "Launch Built-in web server",
            "type": "php",
            "request": "launch",
            "runtimeArgs": [
                "-dxdebug.mode=debug",
                "-dxdebug.start_with_request=yes",
                "-S",
                "localhost:0"
            ],
            "program": "",
            "cwd": "${workspaceRoot}",
            "port": 9003,
            "serverReadyAction": {
                "pattern": "Development Server \\(http://localhost:([0-9]+)\\) started",
                "uriFormat": "http://localhost:%s",
                "action": "openExternally"
            }
        }
    ]
}

// Dockerfile
FROM php:8.2-apache
COPY . /var/www/html
COPY ./httpd-vhost.conf /etc/apache/sites_available/
WORKDIR /var/www/html
RUN apt-get update && apt-get install -y \
        libfreetype6-dev \
        libjpeg62-turbo-dev \
        libpng-dev \
    && docker-php-ext-configure gd --with-freetype --with-jpeg \
    && docker-php-ext-install -j$(nproc) gd \
    && docker-php-ext-install pdo_mysql \
    && a2enmod rewrite \
    && pecl install xdebug-3.3.1 && docker-php-ext-enable xdebug
    # \
    # && sed -i '/LoadModule rewrite_module/s/^#//g' /usr/local/apache2/conf/httpd.conf \
    # && sed -i 's#AllowOverride [Nn]one#AllowOverride All#' /usr/local/apache2/conf/httpd.conf


// docker-compose
version: '3.1'

services:
  php:
    build:
      context: .
      dockerfile: Dockerfile
    volumes:
      [./:/var/www/html,
      ./docker/php/conf.d/php.ini:/usr/local/etc/php/php.ini,
      ./docker/php/conf.d/xdebug.ini:/usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini,
      ./docker/php/conf.d/error_reporting.ini:/usr/local/etc/php/conf.d/error_reporting.ini
      ]
    ports:
      ["8081:80"]
    stdin_open: true
    tty: true
  db:
    image: mariadb:10.6
    restart: always
    volumes:
      ["./mariadb/data:/var/lib/mysql"]
    environment:
      MYSQL_ROOT_PASSWORD: notSecureChangeMe
      MYSQL_USER: product_db_user
      MYSQL_PASSWORD: secret
  phpmyadmin:
    image: phpmyadmin
    restart: always
    ports:
      - 8001:80
    environment:
      - PMA_ARBITRARY=1

// php.ini
(this file is blank, it just was one line that's commented out)

Thanks.

Do you mean PHPStorm as expected but VSCode doesn’t? I don’t think I ever used VScode with PHP, so I don’t think I can help you with that.

What is launch.json. Is that the configuration file for VSCode? I see you confugure localhost, but shouldn’t that be the IP of your host?

Yes, launch.json is a vs code configuration. PHP Storm, on my work computer debugs just fine, but for my own projects and on my own computer I’d rather user a lightweight editor/IDE/debugger like vs code, and free (I hate subscriptions based software). I also do Udemy lessons almsot every Saturday/Sunday morning, but all my php lessons have come to a talk, most due to not being able to move forward with getting my contianer/xdebug/vscode to play together.

I understand you, but if PHPSorm worked and VSCode didn’t, it seems it is a VSCode issue which I can’t help you with. Maybe one day I will try to use VSCode for PHP again, but even though VSCode is really good for many things, I could never get used to it for PHP development trying it for more than a year. Have you tried asking about this issue on a Microsoft forum as well?

Thanks, I finally solved using this guys tutorial: Xdebug in VSCode with Docker - DEV Community
I just removed all my current xdebug settings from every file and applied his, vs code started debugging as soon as I refreshed my page in the browser. So what IDE do you use? Do you debug php with it? I’m always open to suggestions.

1 Like

Thank you for the link.

PHPStorm, but it is just my personal preference. I’m not an active PHP developer anymore, but since I once accidentally bought all products pack instead of renewing only PHPStorm and started to use other IDEs as well, I still have it and sometimes use it. Use whatever works for you. I still use VSCode for other tasks, although I changed the default theme to the Jetbrains Icon theme.

Thanks. I use phpStorm and my job but I am trying to avoid in my personal computer because I hate Saas, if I could pay once and be done with it there’s a good change that’s the route I would take, although I do prefer lightweight editors like atom/vs code over more bulky ones.

You can pay PhpStorm at once. Simply choose a one-year subscription. This includes a perpetual fallback license. The license also includes all bugfix updates. But you won’t receive an update to the next version, which is also usually the case with On-premises software as well.

Unfortunately, Atom IDE has reached the end of its life on December 15, 2023, as the developer GitHub/Microsoft will prioritize GitHub Codespace and Microsoft Visual Studio Code.

An open source alternative is Eclipse PHP Development Tools 8.3

If you prefer “lightweight” editors, you should also take a look at Notepad++ with the DBGp plugin for debugging.