Installing XDebug in Docker

32,850

Solution 1

I solved this by adding the following lines into my Docker file:

FROM php:7.0-apache

RUN a2enmod rewrite

RUN docker-php-ext-install pdo pdo_mysql

RUN yes | pecl install xdebug \
    && echo "zend_extension=$(find /usr/local/lib/php/extensions/ -name xdebug.so)" > /usr/local/etc/php/conf.d/xdebug.ini \
    && echo "xdebug.remote_enable=on" >> /usr/local/etc/php/conf.d/xdebug.ini \
    && echo "xdebug.remote_autostart=off" >> /usr/local/etc/php/conf.d/xdebug.ini


COPY php.ini /usr/local/etc/php/
COPY . /var/www/html/

Solution 2

try this:

RUN pecl install xdebug && docker-php-ext-enable xdebug

Solution 3

As of PHP 7.4 you only need this

RUN pecl install xdebug \
    && docker-php-ext-enable xdebug

And add this line to enable remote debugging

&& echo "xdebug.remote_enable=on" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
&& echo "xdebug.remote_host = host.docker.internal" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \

Solution 4

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

RUN pecl install xdebug; \
    docker-php-ext-enable xdebug

and for configuration:

        { \
            echo "xdebug.mode=debug"; \
            echo "xdebug.start_with_request=yes"; \
            echo "xdebug.client_host=host.docker.internal"; \
            echo "xdebug.client_port=9000"; \
        } > /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini; \

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

Solution 5

For PHP 7.3 I was able to install XDebug using the mlocati's docker-php-extension-installer.

My Dockerfile looks like this:

FROM php:7.3-apache
COPY --from=mlocati/php-extension-installer /usr/bin/install-php-extensions /usr/local/bin/
RUN install-php-extensions xdebug @composer

My docker-compose.yml contains the following which allows the special host.docker.internal name:

extra_hosts:
  - "host.docker.internal:host-gateway" 

My xdebug configuration file /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini looks like this:

[xdebug]
zend_extension=xdebug
xdebug.mode=develop,debug
xdebug.start_with_request = yes
xdebug.client_host = "host.docker.internal"
xdebug.client_ip = "9003"
xdebug.idekey="VSCODE"
xdebug.log=/tmp/xdebug_remote.log

More details are in a Gist describing my PHP Debugging in Docker setup.

Share:
32,850
UrmLmn
Author by

UrmLmn

Updated on July 23, 2022

Comments

  • UrmLmn
    UrmLmn almost 2 years

    I'm trying to install the XDebug in a Docker container, but I'm getting the following error:

    E: Unable to locate package php-xdebug
    

    This is my Dockerfile:

    FROM php:7.0-apache
    
    RUN a2enmod rewrite
    
    RUN docker-php-ext-install pdo pdo_mysql
    
    RUN apt-get install php-xdebug -y
    
    COPY php.ini /usr/local/etc/php/
    COPY . /var/www/html/
    

    When I'm running the same command in my computer, the XDebug is getting installed without any error:

    apt-get install php-xdebug
    

    Where might the problem be?