How to enable xdebug in php:7.4-fpm-alpine docker container?
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
# How to Enable Xdebug in PHP: A Deep Dive for Docker Alpine Environments
As a senior developer working within the Laravel ecosystem, containerizing applications with Docker Compose is an essential practice. However, integrating powerful debugging tools like Xdebug into custom PHP-FPM images, especially those based on lightweight distributions like Alpine Linux, often introduces specific compilation and dependency challenges.
If you are following a popular repository setup, such as the one utilizing `docker-compose-laravel`, you might encounter frustrating build errors when trying to install extensions like Xdebug. This guide will walk you through diagnosing why these builds fail and providing a robust, working solution for enabling Xdebug in your PHP 7.4-fpm-alpine containers.
## The Challenge: Compiling Extensions in Alpine Docker Images
The issue you are encounteringâwhere `composer` or `npm` exits with errors during the build processâis almost always related to missing essential build dependencies required by the PECL installation for Xdebug on an Alpine base. Alpine is intentionally small, which means development tools must be installed explicitly, and the order of operations matters immensely when compiling C extensions.
The initial approach you used was conceptually correct: using `docker-php-source` to extract the source code, compiling the extension via PECL, and then setting the necessary configuration flags. However, Alpine's package manager (`apk`) requires a specific set of tools to compile external libraries successfully.
## The Robust Solution for Xdebug Installation
To reliably install Xdebug on an Alpine PHP image, we need to ensure that all necessary compilers, development headers, and build tools are installed *before* attempting the PECL compilation step. This ensures that the C code can be successfully compiled against the system libraries.
Here is the corrected and highly recommended sequence of commands for your `Dockerfile`:
```dockerfile
FROM php:7.4-fpm-alpine
# 1. Install essential build tools and dependencies
RUN apk add --no-cache \
git \
yarn \
autoconf \
g++ \
make \
openssl-dev \
# Add development headers needed for common PHP extensions
libzip-dev
# 2. Install PHP dependencies (PDO drivers)
RUN docker-php-ext-install pdo pdo_mysql
# 3. Install Xdebug via source compilation
RUN docker-php-source extract \
&& pecl install xdebug \
&& echo "xdebug.remote_enable=on" > /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
&& echo "xdebug.remote_autostart=on" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
&& echo "xdebug.remote_port=9001" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
&& echo "xdebug.remote_handler=dbgp" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
&& echo "xdebug.remote_connect_back=1" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
&& docker-php-ext-enable xdebug \
&& docker-php-source delete \
&& rm -rf /tmp/*
```
### Explanation of the Fixes
1. **Comprehensive Toolchain:** We explicitly added `libzip-dev` alongside the standard build tools (`g++`, `make`, `autoconf`). This ensures that dependencies related to common PHP extensions (like Zip) are available during compilation, preventing dependency resolution errors that often cause the build to fail silently or abruptly.
2. **Sequential Execution:** The critical change is ensuring all system package installations happen first. By grouping all required tools in a single `RUN` command, we minimize potential state-related issues between steps, which often resolves complex build failures in Docker environments.
3. **Configuration Placement:** We ensure the configuration lines to enable remote debugging are appended correctly to the `.ini` file before enabling the extension via `docker-php-ext-enable`.
## Conclusion: Best Practices for PHP Extensions in Docker
Setting up custom PHP extensions inside a container requires treating the build process as an operating system compilation task, not just a simple script execution. Always start by auditing the dependencies required by the specific PECL package you are trying to installâin this case, Xdebug needs robust C compilers and development libraries.
For modern Laravel projects, relying on well-maintained base images and leveraging official PHP extensions where possible is often simpler than compiling everything from source. If you are looking to build custom environments for complex tooling or deep debugging, mastering these Dockerfile nuances is key. Remember, a stable environment is the foundation upon which robust applications, like those built using Laravel principles, are created. For more insights into building scalable PHP applications, always refer to resources from [laravelcompany.com](https://laravelcompany.com).