dockerxdebugvscode-debuggerxdebug-3

Config error Xdebug with Docker and VSCode?


I am in a Docker environment with separation of PHP and Apache containers. I'm using PHP 8.2 and Symfony 7.1. I'm trying to add Xdebug in order to solve a SF code problem.

Anyway, despite the numerous tutorials followed, the VSCode debugger does not react despite my breakpoints.

Here is my Docker configuration:

dockerCompose

version: '3.9'
 
networks:
    docker.network:
        driver: bridge
 
services:
    db:
        image: mysql
        container_name: mysql
        restart: always
        ports:
            - "3306:8080"
        networks:
            - docker.network
        environment:
            MYSQL_ALLOW_EMPTY_PASSWORD: Yes
        volumes:
            - ./db/db_data:/var/lib/mysql
 
    apache:
        image: httpd
        container_name: apache
        ports:
            - "80:80"
            #- "443:443"
        networks:
            - docker.network
        volumes:
            - ../:/var/www
            - ./apache/httpd-vhosts.conf:/usr/local/apache2/conf/extra/httpd-vhosts.conf
            - ./apache/commun.conf:/usr/local/apache2/conf/extra/commun.conf
            - ./apache/httpd.conf:/usr/local/apache2/conf/httpd.conf
            - ./hosts:/etc/hosts  # Monter le fichier hosts
 
 
    php82:
        build: ./php/php8.2
        container_name: php82
        volumes:
           - ../:/var/www
           - ./php/php.ini:/etc/php/8.2/fpm/conf.d/30-custom.ini
           - ./php/php.ini:/etc/php/8.2/cli/conf.d/30-custom.ini
           - ./php/php.ini:/usr/local/etc/php/conf.d/30-custom.ini
           - ./hosts:/etc/hosts  # Monter le fichier hosts
        environment:
            - TZ=Europe/Paris
        ports:
            - "9000:9000"
            - "9004:9004"
        restart: always
        networks:
            - docker.network
        extra_hosts:
            - host.docker.internal:host-gateway
 
    phpmyadmin:
        image: phpmyadmin
        container_name: phpmyadmin
        ports:
            - "86:80"
        networks:
            - docker.network
 
    mailer:
        image: mailhog/mailhog
        container_name: mailer
        ports:
            - "1025:1025"
            - "8025:8025"
        networks:
            - docker.network

DockerFile

# From
FROM phpdockerio/php:8.2-fpm
 
ARG DEBIAN_FRONTEND=noninteractive
 
# Run
RUN apt-get update
 
# Install Nano
RUN apt-get install -y nano
 
RUN apt-get install -y php8.2-mysqli
 
# Install necessary packages for pecl and ODBC
RUN apt-get update && apt-get install -y \
    php-pear \
    php8.2-dev \
    gcc \
    g++ \
    make \
    autoconf \
    libc-dev \
    pkg-config \
    unixodbc-dev \
    libxml2-dev \
    php-xml 
 
RUN apt-get install -y \
    libpng-dev \
    libjpeg-dev \
    libfreetype6-dev \
    libzip-dev \
    zip \
    && pecl install gd \
    && docker-php-ext-enable gd \
    && pecl install mysqli \
    && docker-php-ext-enable mysqli \
    && pecl install pdo \
    && docker-php-ext-enable pdo \
    && pecl install pdo_mysql \
    && docker-php-ext-enable pdo_mysql \
    && pecl install zip \
    && docker-php-ext-enable zip \
    && apt-get clean; rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* /usr/share/doc/*
 
# Install SQL Server extensions
RUN pecl config-set php_ini /etc/php/8.2/fpm/php.ini \
    && pecl install sqlsrv \
    && pecl install pdo_sqlsrv \
    && printf "; priority=20\nextension=sqlsrv.so\n" > /etc/php/8.2/mods-available/sqlsrv.ini \
    && printf "; priority=30\nextension=pdo_sqlsrv.so\n" > /etc/php/8.2/mods-available/pdo_sqlsrv.ini \
    && phpenmod -v 8.2 sqlsrv pdo_sqlsrv
    
# Install necessary packages for MS ODBC Driver
RUN apt-get update && apt-get install -y \
    curl \
    apt-transport-https \
    gnupg
 
# Add Microsoft's repo for the latest ODBC driver for SQL Server
RUN curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
RUN curl https://packages.microsoft.com/config/ubuntu/22.04/prod.list > /etc/apt/sources.list.d/mssql-release.list
 
# Install the MS ODBC driver for SQL Server
RUN apt-get update && ACCEPT_EULA=Y apt-get install -y \
    msodbcsql17 \
    mssql-tools
 
# Ensure the PHP configuration directory exists
RUN mkdir -p /usr/local/etc/php/conf.d
 
# Install Xdebug
RUN pecl install xdebug 
 
# Configure Xdebug
RUN echo "zend_extension=$(find /usr/lib/php/ -name xdebug.so)" > /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
    && echo "xdebug.mode=debug" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
    && echo "xdebug.start_with_request=yes" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
    && echo "xdebug.client_host=host.docker.internal" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
    && echo "xdebug.client_port=9000" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
    && echo "xdebug.log=/var/log/xdebug.log" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
 
# Créer le répertoire de travail
RUN mkdir -p /var/www
 
# Automatiser les permissions du répertoire
RUN chown -R www-data:www-data /var/www \
    && chmod -R 755 /var/www

30-custom.ini

display_errors = On
display_startup_errors = On
log_errors = On
error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT
directorypermissions = "0775"
date.timezone = Europe/Paris
 
max_execution_time = 500
max_input_time = 500
memory_limit = 256M
 
; zend_extension=/usr/lib/php/20220829/xdebug.so
; xdebug.mode=debug
; xdebug.start_with_request=yes
; xdebug.client_host=172.0.0.1
; xdebug.client_port=9004
 
zend_extension=/usr/lib/php/20220829/xdebug.so
; ; zend_extension=xdebug.so
xdebug.mode=debug
xdebug.idekey=XDEBUG_SESSION_START
xdebug.start_with_request=yes
xdebug.log=/dev/stdout
xdebug.log_level=0
xdebug.client_port=9000
xdebug.client_host=host.docker.internal
xdebug.discover_client_host=true
xdebug.remote_host=host.docker.internal

I did a php_info() where I have the instantiation of Xdebug:

enter image description here

I then configured my VSCode with the PHP debug extension, and the debugger settings. Here is my lunch.json file:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Listen for Xdebug",
            "type": "php",
            "request": "launch",
            "port": 9000,
            "hostname": "127.0.0.1",
            "pathMappings": {
                "/var/www": "${workspaceFolder}"
            }
        }
    ]
}

I tested either with POSTMAN or by directly calling the URL http://datalookup.local/index.php/lookup/liste-marques

For browsers, I added the Xdebug helper extension, which is activated. Unfortunately, absolutely nothing happens in VSCode. I tested with another port 9004, which I opened on the docker container, but nothing.

I don't really know where to turn anymore. Can you help me? Do you need additional information?

I tested with different ports, I tested with different keys, and changing networks. I followed various tutorials including that of grafikart.


Solution

  • I finally found it.

    I removed the port declaration in the dockerFile and the php.ini. And I set the default port in lunch.json to be 9003, and for my docker-compose I only left the port for my code to be 9000.

    Then I had a problem with a breakpoint that wasn't recognized or caught, so I changed the pathMappings to target not the root of my dev folder, but my project directly.

    lunch.json

    {
      "version": "0.2.0",
      "configurations": [
         {
            "name": "Listen for Xdebug",
            "type": "php",
            "request": "launch",
            "port": 9003,
            "pathMappings": {
                "/var/www/api-global": "c:/Users/gnicolle/Sites/api-global"
            }
         }
      ]
    }
    

    30-custom.ini

    zend_extension=xdebug.so
    xdebug.mode=develop,debug
    xdebug.start_with_request=yes
    xdebug.log_level=10
    xdebug.log=/var/log/xdebug.log
    xdebug.discover_client_host=0
    xdebug.client_host=host.docker.internal