Xdebug traffic wasn't finding it's way back to my host. When I entered the IP-address of my host machine in xdebug.remote_host, debugging worked but this was not ideal because my IP changes on every reboot. Luckily, Docker provides a way to obtain the host IP via a DNS-entry docker.for.win.localhost.

Edit:
Docker now has a platform-independent DNS-entry host.docker.internal.

I tried to use xdebug.remote_connect_back=1 which just responds to traffic from where it came but it wasn't working. Phpinfo() showed that REMOTE_ADDR was set to localhost. Localhost in this context points to the Docker PHP-container while it should point to our host IP. Important to know with this setting is that xdebug.remote_host in the .ini will be ignored.

From the Xdebug docs:

xdebug.remote_connect_back Type: boolean, Default value: 0, Introduced in Xdebug >= 2.1

If enabled, the xdebug.remote_host setting is ignored and Xdebug will try to connect to the client that made the HTTP request.

Here is the Dockerfile with the xdebug.ini configuration for the PHP Docker container that worked:

FROM php:7.1-fpm
ARG TIMEZONE

MAINTAINER Maxence POUTORD <[email protected]>

RUN apt-get update && apt-get install -y \
    openssl \
    git \
    unzip

# Install Composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
RUN composer --version

# Set timezone
RUN ln -snf /usr/share/zoneinfo/${TIMEZONE} /etc/localtime && echo ${TIMEZONE} > /etc/timezone
RUN printf '[PHP]\ndate.timezone = "%s"\n', ${TIMEZONE} > /usr/local/etc/php/conf.d/tzone.ini
RUN "date"

# Type docker-php-ext-install to see available extensions
RUN docker-php-ext-install pdo pdo_mysql

# install xdebug
RUN pecl install xdebug
RUN docker-php-ext-enable xdebug
RUN echo "zend_extension=/usr/local/lib/php/extensions/no-debug-non-zts-20160303/xdebug.so" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
RUN echo "error_reporting = E_ALL" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
RUN echo "display_startup_errors = On" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
RUN echo "display_errors = On" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini

# relevant to this answer
RUN echo "xdebug.idekey=\"PHPSTORM\"" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
RUN echo "xdebug.remote_port=9000" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
RUN echo "xdebug.remote_enable=1" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
RUN echo "xdebug.remote_autostart=1" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
RUN echo "xdebug.remote_host=docker.for.win.localhost" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini

RUN echo 'alias sf="php app/console"' >> ~/.bashrc
RUN echo 'alias sf3="php bin/console"' >> ~/.bashrc

WORKDIR /var/www/free-energy/symfony


Edit:
I was messing around with XDebug quite a bit in this post.
Now I know your XDebug config doesn't have to be as big as I had back then.

xdebug.ini: (in your Docker PHP image).

zend_extension=xdebug.so

[Xdebug]
xdebug.remote_enable=true
xdebug.remote_port=9000
xdebug.remote_host=host.docker.internal
Answer from progonkpa on Stack Overflow
Top answer
1 of 2
10

Xdebug traffic wasn't finding it's way back to my host. When I entered the IP-address of my host machine in xdebug.remote_host, debugging worked but this was not ideal because my IP changes on every reboot. Luckily, Docker provides a way to obtain the host IP via a DNS-entry docker.for.win.localhost.

Edit:
Docker now has a platform-independent DNS-entry host.docker.internal.

I tried to use xdebug.remote_connect_back=1 which just responds to traffic from where it came but it wasn't working. Phpinfo() showed that REMOTE_ADDR was set to localhost. Localhost in this context points to the Docker PHP-container while it should point to our host IP. Important to know with this setting is that xdebug.remote_host in the .ini will be ignored.

From the Xdebug docs:

xdebug.remote_connect_back Type: boolean, Default value: 0, Introduced in Xdebug >= 2.1

If enabled, the xdebug.remote_host setting is ignored and Xdebug will try to connect to the client that made the HTTP request.

Here is the Dockerfile with the xdebug.ini configuration for the PHP Docker container that worked:

FROM php:7.1-fpm
ARG TIMEZONE

MAINTAINER Maxence POUTORD <[email protected]>

RUN apt-get update && apt-get install -y \
    openssl \
    git \
    unzip

# Install Composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
RUN composer --version

# Set timezone
RUN ln -snf /usr/share/zoneinfo/${TIMEZONE} /etc/localtime && echo ${TIMEZONE} > /etc/timezone
RUN printf '[PHP]\ndate.timezone = "%s"\n', ${TIMEZONE} > /usr/local/etc/php/conf.d/tzone.ini
RUN "date"

# Type docker-php-ext-install to see available extensions
RUN docker-php-ext-install pdo pdo_mysql

# install xdebug
RUN pecl install xdebug
RUN docker-php-ext-enable xdebug
RUN echo "zend_extension=/usr/local/lib/php/extensions/no-debug-non-zts-20160303/xdebug.so" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
RUN echo "error_reporting = E_ALL" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
RUN echo "display_startup_errors = On" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
RUN echo "display_errors = On" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini

# relevant to this answer
RUN echo "xdebug.idekey=\"PHPSTORM\"" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
RUN echo "xdebug.remote_port=9000" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
RUN echo "xdebug.remote_enable=1" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
RUN echo "xdebug.remote_autostart=1" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
RUN echo "xdebug.remote_host=docker.for.win.localhost" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini

RUN echo 'alias sf="php app/console"' >> ~/.bashrc
RUN echo 'alias sf3="php bin/console"' >> ~/.bashrc

WORKDIR /var/www/free-energy/symfony


Edit:
I was messing around with XDebug quite a bit in this post.
Now I know your XDebug config doesn't have to be as big as I had back then.

xdebug.ini: (in your Docker PHP image).

zend_extension=xdebug.so

[Xdebug]
xdebug.remote_enable=true
xdebug.remote_port=9000
xdebug.remote_host=host.docker.internal
2 of 2
1

Add the remote_connect_back=on, so xdebug connects to the current visitor (using the REMOTE_ADDR system variable) instead of the remote_host which may change. You can try with remote_autostart=on too, to make sure this issue is not related with the browser itself.

Remember to configure the mapping in phpStorm as well, but it will tell you to do so anyway. Also make sure the IDE is listening to the debug session (small toggle in the debug toolbar). Good luck.

Discussions

IDE debugging with Docker, nginx, PHP7-FPM and Xdebug - Stack Overflow
I'm currently preparing a development stack on macOS with docker-compose to be able to use Xdebug (Port: 9009) on PHP7-FPM (Port: 9000) and nginx (Port: 80) server. More on stackoverflow.com
🌐 stackoverflow.com
laravel - How to enable xdebug in php:7.4-fpm-alpine docker container? - Stack Overflow
My target is to use this git repo for Laravel with xdebug for php-fpm: https://github.com/aschmelyun/docker-compose-laravel When using this repo i run: docker-compose up -d --build site docker-com... More on stackoverflow.com
🌐 stackoverflow.com
How do I install XDebug on docker's official php-fpm-alpine image? - Stack Overflow
If you use a non-Alpine-based base image, use apt instead of apk, e.g. RUN apt -qy install $PHPIZE_DEPS && pecl install xdebug-2.5.0 && docker-php-ext-enable xdebug 2019-09-03T14:32:42.233Z+00:00 ... in my case it runs perfectly with CLI, but won't run with FPM. More on stackoverflow.com
🌐 stackoverflow.com
php - Installing XDebug in Docker - Stack Overflow
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 ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
GitHub
github.com › maciejslawik › docker-php-fpm-xdebug › blob › master › Dockerfile
docker-php-fpm-xdebug/Dockerfile at master · maciejslawik/docker-php-fpm-xdebug
&& docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \ ... && echo "xdebug.profiler_output_dir=/tmp/snapshots" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
Author   maciejslawik
🌐
Matthew Setter
matthewsetter.com › setup-step-debugging-php-xdebug3-docker
Setup Step Debugging in PHP with Xdebug 3 and Docker Compose | Blog - Matthew Setter. Accessible Web App Developer, Educator, and Author, based in Bundaberg, Queensland.
March 10, 2021 - This is because Xdebug doesn't come "bundled" with the official Docker Hub PHP containers. FROM php:7.4-fpm RUN pecl install xdebug \ && docker-php-ext-enable xdebug
🌐
Medium
medium.com › the-sensiolabs-tech-blog › how-to-use-xdebug-in-docker-phpstorm-76d998ef2534
How to use Xdebug in Docker & PhpStorm | The SensioLabs Tech Blog
June 14, 2022 - So, let’s consider this simple Dockerfile as our base. ... It is missing quite a few things to make our app actually work, but I’m only showing the relevant parts. Now let’s add Xdebug inthere · COPY --from=mlocati/php-extension-installer /usr/bin/install-php-extensions /usr/bin/RUN install-php-extensions xdebug
🌐
GitHub
github.com › serversideup › docker-php › discussions › 176
Xdebug with fpm-nginx · serversideup/docker-php · Discussion #176
Now that install-php-extensions is included in the v3 image, I use this much simpler fpm-nginx_xdebug.dockerfile : ARG PHP_VERSION FROM serversideup/php:${PHP_VERSION}-fpm-nginx USER root RUN install-php-extensions gd xdebug RUN echo "xdebug.mode=debug" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \ && echo "xdebug.start_with_request=trigger" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \ && echo "xdebug.trigger_value=PHPSTORM" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \ && echo "xdebug.client_port=9003" >> /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 # Expose port 9003 for Xdebug EXPOSE 9003 USER www-data
Author   serversideup
🌐
DEV Community
dev.to › pimenvibritania › debug-docker-php-project-on-phpstorm-using-xdebug-224f
Debug Docker PHP Project on PHPStorm using Xdebug - DEV Community
December 4, 2021 - FROM php:8.1.0-fpm ADD https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions /usr/local/bin/ RUN apt-get update && apt-get install -y \ git \ curl \ zip \ nano \ vim \ unzip RUN chmod +x /usr/local/bin/install-php-extensions && \ install-php-extensions gd xdebug pdo-mysql RUN docker-php-ext-install pdo pdo_mysql RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" RUN php composer-setup.php --install-dir=. --filename=composer RUN mv composer /usr/local/bin/ COPY ../ /var/www/ WORKDIR /var/www EXPOSE 9000
Find elsewhere
🌐
Thecodingmachine
thecodingmachine.io › configuring-xdebug-phpstorm-docker
Debugging PHP (web and cli) with Xdebug using Docker and PHPStorm
July 26, 2018 - Debugging PHP (web and cli) with Xdebug using Docker and PHPStorm ... To follow this tutorial, you must have the Xdebug extension installed on your container. In my example, I will use a Docker image of TheCodingMachine created by David Négrier.
🌐
Laravel News
laravel-news.com › home › laravel tutorials › get xdebug working with docker and php 8.4 in one minute
Get Xdebug Working With Docker and PHP 8.4 in One Minute - Laravel News
December 31, 2024 - If you open your project in a web browser (localhost:8080), PhpStorm should prompt you to connect the first time. After that, it will automatically pause on any breakpoints you set. Once you are done debugging, you can click the bug icon to disable Xdebug connections. Continue in Part 2 of this tutorial: Flexible Docker Images with PHP INI Environment Variables.
🌐
Medium
medium.com › @fico7489 › install-xdebug-in-your-docker-php-container-and-then-enable-and-disable-any-time-when-you-want-d8ac31ae53f4
Install “xdebug” in your docker PHP container and then enable and disable any time when you want | by Filip Horvat | Medium
February 20, 2023 - “app_php_dev” —dev image where xdebug is installed “app_php_dev_performance” —dev image where xdebug is not installed “app_php_prod” — production image · We will add this 3 stages into the .docker/php/Dockerfile: FROM php:8.1-fpm-alpine AS app_php RUN echo ‘app_php’ FROM app_php AS app_php_dev RUN echo 'app_php_dev' #define custom options for "app_php_dev" FROM app_php AS app_php_dev_performance RUN echo ‘app_php_dev_performance’ #define custom options for “app_php_dev_performance” FROM app_php AS app_php_prod RUN echo 'app_php_prod' #define custom options for "app_php_prod"
🌐
Peterbabic
peterbabic.com › blog › php-xdebug-in-docker
Peter Babič - PHP xDebug in Docker
February 23, 2025 - RUN apk add --no-cache $PHPIZE_DEPS linux-headers && \ pecl install xdebug && docker-php-ext-enable xdebug · Base for the docker-compose.yml is below. Note the .ini files in volumes: version: "3.9" services: app: build: context: ./ dockerfile: Dockerfile image: php-fpm-81 container_name: my-app ...
🌐
Medium
medium.com › @adev95 › install-xdebug-for-php-7-on-docker-3e47b3e5de9c
Install XDebug for PHP 7 on Docker 🪲 | by Adev95 | Medium
October 3, 2023 - Install XDebug for PHP 7 on Docker 🪲 This tutorial is for an container that has PHP 7.1 FPM and needs to install XDebug extension. Also include the configuration for setup the …
🌐
GitHub
github.com › maciejslawik › docker-php-fpm-xdebug
GitHub - maciejslawik/docker-php-fpm-xdebug: Docker image: PHP-FPM with XDEBUG enabled.
Docker image: PHP-FPM with XDEBUG enabled. Contribute to maciejslawik/docker-php-fpm-xdebug development by creating an account on GitHub.
Starred by 25 users
Forked by 12 users
Languages   Dockerfile 100.0% | Dockerfile 100.0%
🌐
JetBrains
intellij-support.jetbrains.com › hc › en-us › community › posts › 6599108484370-How-do-I-configure-xDebug-in-PHPStorm-in-using-Docker-containers
How do I configure xDebug in PHPStorm in using Docker containers? – IDEs Support (IntelliJ Platform) | JetBrains
July 13, 2022 - Just as a starting point, I HAVE managed to install xDebug with this in my php.dockerfile: FROM php:8-fpm-alpine ENV PHPGROUP=laravel ENV PHPUSER=laravel RUN adduser -g ${PHPGROUP} -s /bin/sh -D ${PHPUSER} RUN sed -i "s/user = www-data/user = ${PHPUSER}/g" /usr/local/etc/php-fpm.d/www.conf RUN sed -i "s/group = www-data/group = ${PHPGROUP}/g" /usr/local/etc/php-fpm.d/www.conf RUN mkdir -p /var/www/html/public RUN docker-php-ext-install pdo pdo_mysql RUN apk add --no-cache $PHPIZE_DEPS \ && 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 COPY --from=composer:latest /usr/bin/composer /usr/local/bin/composer CMD ["php-fpm", "-y", "/usr/local/etc/php-fpm.conf", "-R"]
🌐
GitHub
gist.github.com › esilvajr › df244d5c8459403ee68eaf83773054e4
How to use XDebug inside a docker container. · GitHub
In database and zero-config we don't need anything. Just let them, like they are by default. Now we need to configure the docker-compose.yaml and Dockerfile, first open the /phpdocker.io/php-fpm/Dockerfile ...