Debugging PHP with Xdebug and VS Code on Docker
1. Create ext-xdebug.ini file with the following content
docker/8.1/ext-xdebug.ini
zend_extension=xdebug.so
xdebug.mode=develop,coverage,debug,profile
xdebug.idekey=docker
xdebug.start_with_request=yes
xdebug.log=/dev/stdout
xdebug.log_level=0
xdebug.client_port=9003
xdebug.client_host=host.docker.internal
xdebug.discover_client_host=true
2. Install the Xdebug extension and include the configurations in the Dockerfile
RUN pecl install xdebug && docker-php-ext-enable xdebug

COPY docker/8.1/ext-xdebug.ini /usr/local/etc/php/conf.d/xdebug.ini
3. Rebuild all the containers
  1. docker build --no-cache -t [IMAGE] .
4. Install PHP Debug Adapter for Visual Studio Code 5. Add the debugging configuration to Visual Studio Code
.vscode/launch.json
{
    "name": "Listen for Xdebug",
    "type": "php",
    "request": "launch",
    "port": 9003,
    "pathMappings": {
        "/var/www/html": "${workspaceFolder}",
    },
}
6. Check if Xdebug is working correctly