'Installing XDebug in Docker

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?



Solution 1:[1]

try this:

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

Solution 2:[2]

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 3:[3]

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 4:[4]

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.

Solution 5:[5]

I was using php8.0-alpine in the dockerfile. I was having an issue in installing xdebug in the container, so I changed the image to php8.0-alpine3.13 and works fine. Seems like 3.14 was having some issue with xdebug.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mitja
Solution 2
Solution 3
Solution 4 Chris Hirt
Solution 5 Anurag Prashant