Setting up XDebug with Docker and VSCode

I set up a Laravel dev environment using Docker – nginx:stable-alpine, php:8.0-fpm-alpine and mysql:5.7.32. I install Xdebug from my php.dockerfile:

RUN apk --no-cache add pcre-dev ${PHPIZE_DEPS}  
&& pecl install xdebug 
&& docker-php-ext-enable xdebug 
&& apk del pcre-dev ${PHPIZE_DEPS}

And include two volumes in docker-compose to point php to xdebug.ini and error_reporting.ini:

    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/error_reporting.ini:/usr/local/etc/php/conf.d/error_reporting.ini

My xdebug.ini looks like this:

zend_extension=xdebug

[xdebug]
xdebug.mode=develop,debug,trace,profile,coverage
xdebug.start_with_request = yes
xdebug.discover_client_host = 0
xdebug.remote_connect_back = 1 
xdebug.client_port = 9003
xdebug.remote_host='host.docker.internal'
xdebug.idekey=VSCODE

When I return phpinfo() I can see that everything looks set up correctly, showing xdebug version 3.0.4 is installed, but when I set a breakpoint in VSCode and run the debugger its not hitting it.

My launch.json looks like this:

{
"version": "0.2.0",
"configurations": [
    {
        "name": "XDebug Docker",
        "type": "php",
        "request": "launch",
        "port": 9003,
        "pathMappings": {
            "/var/www/html": "${workspaceFolder}/src",
        }
    },
]

}

My folder structure looks like this:

- Project
-- /docker
--   nginx.dockerfile
--   php.dockerfile
--- /nginx
---  /certs
---  default.conf
--- /php
---- /conf.d
----   error_reporting.ini
----   xdebug.ini
-- /src (the laravel app)

Source: Docker Questions

LEAVE A COMMENT