How do I install XDebug on docker's official php-fpm-alpine image?

30,351

Solution 1

The following is sufficient for simply installing xdebug on that image:

FROM wordpress:php7.1-fpm-alpine

RUN apk add --no-cache $PHPIZE_DEPS \
    && pecl install xdebug-2.5.0 \
    && docker-php-ext-enable xdebug

Building that and then running from a shell inside the resulting image produces the following:

$ php -i | grep Xdebug
    with Xdebug v2.5.0, Copyright (c) 2002-2016, by Derick Rethans

Solution 2

If you are concerned about image size you can remove the dependencies:

FROM wordpress:php7.1-fpm-alpine 
RUN apk --update --no-cache add autoconf g++ make && \
    pecl install -f xdebug && \
    docker-php-ext-enable xdebug && \
    apk del --purge autoconf g++ make

Solution 3

Great answer @msanchez_aplyca. Although more correctly removing the build dependancies via apk would be:

RUN apk add --no-cache --virtual .build-deps $PHPIZE_DEPS \
    && pecl install xdebug-2.5.0 \
    && docker-php-ext-enable xdebug \
    && apk del -f .build-deps

Solution 4

For PHP >= 7.2 you need to use Xdebug 2.6.0+

For example, install the Xdebug 3.0.0 (released on the 25th November 2020)

Compatible with PHP 8.0

RUN apk add --no-cache --virtual .build-deps $PHPIZE_DEPS \
    && pecl install xdebug-3.0.0 \
    && docker-php-ext-enable xdebug \
    && apk del -f .build-deps

Now you set it up by adding something like (using Xdebug 3.0.0 syntax, more info here):

# Configure Xdebug
RUN echo "xdebug.start_with_request=yes" >> /usr/local/etc/php/conf.d/xdebug.ini \
    && echo "xdebug.mode=debug" >> /usr/local/etc/php/conf.d/xdebug.ini \
    && echo "xdebug.log=/var/www/html/xdebug/xdebug.log" >> /usr/local/etc/php/conf.d/xdebug.ini \
    && echo "xdebug.discover_client_host=1" >> /usr/local/etc/php/conf.d/xdebug.ini \
    && echo "xdebug.client_port=9000" >> /usr/local/etc/php/conf.d/xdebug.ini
Share:
30,351
Chris Stryczynski
Author by

Chris Stryczynski

Software dev(op). Independent consultant available for hire! Checkout my GitChapter project on github!

Updated on December 05, 2020

Comments

  • Chris Stryczynski
    Chris Stryczynski over 3 years

    I'm using wordpress:php7.1-fpm-alpine which is based on php:7.1-fpm-alpine (https://github.com/docker-library/wordpress/blob/master/php7.1/fpm-alpine/Dockerfile).

    I've tried RUN pecl install xdebug-2.5.0 && docker-php-ext-enable xdebug

    which results in an error when building:

    Step 19/19 : RUN pecl install xdebug-2.5.0     && docker-php-ext-enable xdebug
     ---> Running in 52c988e12cb2
    downloading xdebug-2.5.0.tgz ...
    Starting to download xdebug-2.5.0.tgz (267,640 bytes)
    ........................................................done: 267,640 bytes
    76 source files, building
    running: phpize
    Configuring for:
    PHP Api Version:         20160303
    Zend Module Api No:      20160303
    Zend Extension Api No:   320160303
    Cannot find autoconf. Please check your autoconf installation and the
    $PHP_AUTOCONF environment variable. Then, rerun this script.