Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debug PHP with VSCode and Docker

I'm trying to debug a PHP app running on Docker with VSCode, but without success.

In the past I was able to easily debug my PHP apps with VSCode running WAMP Server, but since I started working with Docker I'm unable to get debug working. Searched for several tutorials online, checked some threads here on StackOverflow (ex.: Docker and XDebug not reading breakpoints VSCode), but I'm still not able to get this working.

Dockerfile:

FROM php:7.1.8-apache  COPY /cms /srv/app/cms COPY .docker/cms/vhosts/vhost.conf /etc/apache2/sites-available/cms.conf COPY .docker/cms/vhosts/vhost-ssl.conf /etc/apache2/sites-available/cms-ssl.conf COPY .docker/cms/vhosts/certificate.conf /etc/ssl/certs/certificate.conf COPY .docker/cms/xdebug.ini /usr/local/etc/php/conf.d/xdebug.ini  WORKDIR /srv/app/cms  RUN docker-php-ext-install mbstring pdo pdo_mysql RUN pecl install xdebug  RUN docker-php-ext-enable xdebug RUN chown -R www-data:www-data /srv/app/cms RUN openssl req -x509 -new -out /etc/ssl/certs/ssl-cert-cms.crt -config /etc/ssl/certs/certificate.conf RUN a2ensite cms.conf RUN a2ensite cms-ssl.conf RUN a2enmod rewrite RUN a2enmod ssl 

xdebug.ini

[xdebug] xdebug.default_enable=1 xdebug.remote_enable=1 xdebug.remote_port=9000 xdebug.remote_connect_back=0 xdebug.remote_host='host.docker.internal' xdebug.idekey='VSCODE' xdebug.remote_autostart=1 

docker-compose.yml

version: '3.7' services: cms:   build:     context: .     dockerfile: .docker/cms/Dockerfile   image: php:7.1.8-apache   ports:     - 18080:80     - 14430:443   volumes:     - ./cms:/srv/app/cms   links:     - mysql     - redis   environment:     DB_HOST: mysql     VIRTUAL_HOST: my.app.localhost     PHP_EXTENSION_XDEBUG: 1 

VSCode: launch.json

"configurations": [     {         "name": "Listen for XDebug",         "type": "php",         "request": "launch",         "pathMappings": {            "/srv/app/cms": "${workspaceRoot}/my.app/cms",         },         "port": 9000     }, {         "name": "Launch currently open script",         "type": "php",         "request": "launch",         "program": "${file}",         "cwd": "${fileDirname}",         "port": 9000     } ] 

When I debug the app no breakpoint is being triggered. What am I doing wrong?

UPDATE: Based on some suggestions i've updated my docker-compose.yml and my launch.json files but nothing changed.

docker-compose.yml

ports:   - 18080:80   - 14430:443   - 9000:9000 //added new xdebug default port 

launch.json

"configurations": [     {         "name": "Listen for XDebug",         "type": "php",         "request": "launch",         "pathMappings": {            "/srv/app/cms": "${workspaceRoot}/my.app/cms",         },         "port": 9000,         "log": true     } ] 

VSCode Debug Console:

<- launchResponse Response { seq: 0, type: 'response', request_seq: 2, command: 'launch', success: true } 

UPDATE #2: Removed the Xdebug port (9000) from the docker-compose.yml settings. Here is the xdebug log result:

Log opened at 2018-09-30 22:21:09 I: Connecting to configured address/port: host.docker.internal:9000. E: Time-out connecting to client (Waited: 200 ms). :-( Log closed at 2018-09-30 22:21:09

Log opened at 2018-09-30 22:21:17 I: Connecting to configured address/port: host.docker.internal:9000. E: Time-out connecting to client (Waited: 200 ms). :-( Log closed at 2018-09-30 22:21:17

Log opened at 2018-09-30 22:21:18 I: Connecting to configured address/port: host.docker.internal:9000. E: Time-out connecting to client (Waited: 200 ms). :-( Log closed at 2018-09-30 22:21:18

Log opened at 2018-09-30 22:21:18 I: Connecting to configured address/port: host.docker.internal:9000. E: Time-out connecting to client (Waited: 200 ms). :-( Log closed at 2018-09-30 22:21:18

Any more suggestions?

like image 213
Ricky Avatar asked Sep 30 '18 15:09

Ricky


People also ask

Can you debug PHP in Visual Studio Code?

PHP debugging with XDebug is supported through a PHP Debug extension. Follow the extension's instructions for configuring XDebug to work with VS Code.

Can Docker run PHP?

We can run php application using docker. In the following steps, we are creating and running php application. Create a directory to organize files by using following command.

Can I run Docker in VS Code?

On Linux, you should also enable Docker CLI for the non-root user account that will be used to run VS Code. To install the extension, open the Extensions view (Ctrl+Shift+X), search for docker to filter results and select Docker extension authored by Microsoft.


2 Answers

Managed to solve my issue with the following settings:

launch.json

{     "version": "0.2.0",     "configurations": [{             "name": "Listen for XDebug",             "type": "php",             "request": "launch",             "port": 9000,             "log": true,             "externalConsole": false,             "pathMappings": {                 "/srv/app/cms": "${workspaceRoot}/cms",             },             "ignore": [                 "**/vendor/**/*.php"             ]         },     ] } 

xdebug.ini

zend_extension=/usr/local/lib/php/extensions/no-debug-non-zts-20160303/xdebug.so xdebug.default_enable=1 xdebug.remote_enable=1 xdebug.remote_port=9000 xdebug.remote_handler=dbgp xdebug.remote_connect_back=0 xdebug.remote_host=host.docker.internal xdebug.idekey=VSCODE xdebug.remote_autostart=1 xdebug.remote_log=/usr/local/etc/php/xdebug.log 
like image 168
Ricky Avatar answered Oct 14 '22 04:10

Ricky


Since xdebug version 3 there are breaking changes in config names.

My current working dockerfile:

RUN apt-get update; \         apt-get -y --no-install-recommends install \             php7.4-dev \             php-pear \             libcurl3-openssl-dev \             libmcrypt-dev \             libxml2-dev \             libpng-dev \         ; \         pecl install xdebug; \         { \             echo "[xdebug]"; \             echo "zend_extension=$(find /usr/lib/php/ -name xdebug.so)"; \             echo "xdebug.mode=debug"; \             echo "xdebug.start_with_request=yes"; \             echo "xdebug.client_host=host.docker.internal"; \             echo "xdebug.client_port=9001"; \         } >> /etc/php/7.4/mods-available/xdebug.ini; \         phpenmod -v 7.4 xdebug; \ 

VSC debug config:

    "configurations": [         {             "name": "XDebug (Docker)",             "type": "php",             "request": "launch",             "port": 9001,             "pathMappings": {                 "/var/www/app": "${workspaceRoot}",             }         }     ] 

More info: https://xdebug.org/docs/upgrade_guide

like image 35
GetoX Avatar answered Oct 14 '22 03:10

GetoX