31

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?

0

7 Answers 7

51

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

# "xdebug-2.9.0" for PHP<=7.4 — "xdebug" (3) for PHP>=8
ARG XDEBUG_VERSION="xdebug-2.9.0"

FROM php:7.0-apache

RUN a2enmod rewrite

RUN docker-php-ext-install pdo pdo_mysql

RUN yes | pecl install ${XDEBUG_VERSION} \
    && 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/
Sign up to request clarification or add additional context in comments.

8 Comments

Hi, when I use this with localhost, the webserver does not respond. Do you have any hint, what I cold do?
@Bernhard, perhaps you forgot to bidn ports with -p when starting the container? Either way, you can start a new question with your full dockerfile and commands that you use :-).
As of now we can shorten some of this to RUN pecl install xdebug && docker-php-ext-enable xdebug
In my case, this command helped RUN pecl install xdebug-2.9.0 && docker-php-ext-enable xdebug
Default version changed and will fail with PHP 7.x. — For PHP 7.4 install xdebug-2.9.0, while for PHP 8.x install xdebug (will default to version 3.x) [at least, as in 2023]
|
40

try this:

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

1 Comment

Please note for PHP 5.x you have to explicitly specify version pecl install xdebug-2.5.5 since newer versions of xdebug don't support 5.x any more
24

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 \

1 Comment

i guess that host.docker.internal is mac only ... if you're on ubuntu you should use 172.17.0.1
9

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

4 Comments

Be aware that another one of the breaking changes is that the default port has changed from 9000 to 9003, so if your IDE has not been updated and now (also) listens to 9003 that last echo line is necessary, but better update or change your IDE configuration.
Would you mind explaining where the "and for configuration" section belongs?
It's a bash script (for dockerfile) that automatically put proper php/xdebug configuration into docker-php-ext-xdebug.ini file.
@MikeiLL if you use a Dockerfile like me, you can put the xdebug config settings into it like this: RUN echo "xdebug.mode=debug" >> $PHP_INI_DIR/conf.d/xdebug.ini ...
6

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.

2 Comments

xdebug.client_ip: you mean xdebug.client_port I hope?
thanks man extra_hosts: - "host.docker.internal:host-gateway" this is what I'm missing in my setup
0

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.

Comments

0

Just a note that use of xdebug.remote_enable and xdebug.remote_host is no longer correct as these options are renamed in xdebug v3 and now you should to use config names instead:

# For xdebug v3
RUN pecl install xdebug \
    && docker-php-ext-enable xdebug \
    && echo "xdebug.mode=debug" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
    && echo "xdebug.client_host = host.docker.internal" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \

Docs:

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.