dockerphpunitphp-7.4

Installing Xdebug in docker for PHP 7.4


I'm attempting to setup unit tests and test coverage for a PHP 7.4 project using PHPUnit 9.x. I've been able to get it working locally, but I'm having a difficult time getting it running in a pipeline runner on GitLab, which is just using Docker under the hood. I'm using the php:7.4 image. At this point, the tests are running, but a coverage report is skipped with the warning:

No code coverage driver available

I've been trying to get Xdebug 3.1.5 installed, as it's the latest version that supports PHP 7.4. From a number of other threads on Stack Overflow and other sites, it appears that most people are suggesting running these two commands:

pecl install xdebug-3.1.5
docker-php-ext-install xdebug

When these are run, the second one errors out with

/usr/src/php/ext/xdebug does not exist

Running docker-php-ext-install xdebug-3.1.5 gives the same kind of error. I've also tried skipping the second command altogether, but I still get the warning from phpunit about no coverage driver.

How might I go about getting xdebug-3.1.5 installed so I can get a coverage report from PHPUnit?


Solution

  • As the name points out the command docker-php-ext-install is meant to install a PHP extension – an .so file. Using PECL is just another way of installing extensions, so, by trying to use docker-php-ext-install after a pecl install you are trying to do twice the same thing.

    And as the end of the pecl install points out:

    You should add "zend_extension=/usr/local/lib/php/extensions/no-debug-non-zts-20190902/xdebug.so" to php.ini

    Which would be what docker-php-ext-enable is meant to do.

    So, your working Dockerfile would be:

    FROM php:7.4
    
    RUN pecl install xdebug-3.1.5 \
          && docker-php-ext-enable xdebug
    

    This is actually pointed out in the documentation of the php image:

    PECL extensions

    Some extensions are not provided with the PHP source, but are instead available through PECL. To install a PECL extension, use pecl install to download and compile it, then use docker-php-ext-enable to enable it:

    FROM php:7.4-cli
    RUN pecl install redis-5.1.1 \
          && pecl install xdebug-2.8.1 \
          && docker-php-ext-enable redis xdebug
    

    Source: https://hub.docker.com/_/php