bpds
bpds

Reputation: 672

using XDebug in a Dockerized PHP on mac OS

The issue

I'm not able to debug using Xdebug from a Dockerized container on macOS.

I ve read very carefully : XDebug Remote configuration, Nikita Nikitin post And all the proposed solutions on Stackoverflow and Github. I'm still blocked..

In the container

I can connect to the container bash -c "clear && docker exec -it DockerizedSample sh"

It confirms that XDebug is installed.

# php -m -c
[PHP Modules]
Core
...
xdebug
...

[Zend Modules]
Xdebug

And its configuration seems valid.

# tail /usr/local/etc/php/conf.d/xdebug.ini

xdebug.idekey=PHPSTORM
xdebug.remote_host=172.17.0.1
xdebug.remote_enable=1
xdebug.remote_mode=req
xdebug.remote_port=9000
xdebug.remote_autostart=0
xdebug.remote_connect_back=0
zend_extension=/usr/local/lib/php/extensions/no-debug-non-zts-20131226/xdebug.so

The docker file

FROM bartlebys/php-apache-mongo:latest
MAINTAINER Benoit Pereira da Silva <https://pereira-da-silva.com>

COPY /html /var/www/html/

################################
## CONFIGURE AND ENABLE XDEBUG #
################################

#Erase the current Configuration of xdebug
RUN     echo "" > /usr/local/etc/php/conf.d/xdebug.ini

#Configure XDEBUG
RUN     HOST_IN_CONTAINER_IP=$(/sbin/ip route|awk '/default/ { print $3 }')\
        &&echo "xdebug.idekey=PHPSTORM" >> /usr/local/etc/php/conf.d/xdebug.ini \
        && echo "xdebug.remote_host=$HOST_IN_CONTAINER_IP" >> /usr/local/etc/php/conf.d/xdebug.ini \
        && echo "xdebug.remote_enable=1" >> /usr/local/etc/php/conf.d/xdebug.ini \
        && echo "xdebug.remote_mode=req" >> /usr/local/etc/php/conf.d/xdebug.ini \
        && echo "xdebug.remote_port=9000" >> /usr/local/etc/php/conf.d/xdebug.ini \
        && echo "xdebug.remote_autostart=0" >> /usr/local/etc/php/conf.d/xdebug.ini\
        && echo "xdebug.remote_connect_back=0" >> /usr/local/etc/php/conf.d/xdebug.ini

# Enable XDEBUG's extension
RUN    echo "zend_extension=$(find /usr/local/lib/php/extensions/ -name xdebug.so)" >> /usr/local/etc/php/conf.d/xdebug.ini

The run script

#!/bin/sh

# Stop the container
echo "Stop "
docker stop DockerizedSample

# Delete the container
echo "Remove "
docker rm DockerizedSample

# Delete the image if necessary
docker rmi dockerizedsampleimage:latest

# Build the youdubserver image
echo "Building with the current source"
docker build -t dockerizedsampleimage:latest .

# Run DockerizedSample container
echo "Run container "

# Run the container once.
# then grab the IP of the HOST in the container
# stop and relaunch with the good IP
docker run  -d --name DockerizedSample dockerizedsampleimage
HOST_IN_CONTAINER_IP=$(docker exec DockerizedSample /sbin/ip route|awk '/default/ { print $3 }')
docker stop DockerizedSample
docker rm DockerizedSample

# Run the debuggable container
docker run  -e PHP_IDE_CONFIG="serverName=Dockerized"\
            -e XDEBUG_CONFIG="remote_host=$HOST_IN_CONTAINER_IP"\
            -p 27017:27017 \
            -p 8001:80\
            -d --name DockerizedSample dockerizedsampleimage\

# Start mongod
echo "Start mongod "
docker exec DockerizedSample service mongod start

echo "IP in Docker Host"
echo "$HOST_IN_CONTAINER_IP"

echo "Local IP"
ipconfig getifaddr en0

# Open localhost in a browser on macOS
if [[ "$OSTYPE" =~ ^darwin ]];
    then open http://localhost:8001/
fi;

How to Reproduce the issue

  1. Download Php-Apache-Mongo/zip/master
  2. Go to the PHPStorm folder and run the shell script ./run.sh

After building the image (may take a few minutes) and running the container it should open a browser on http://localhost:8001/

My current testing environment

Upvotes: 2

Views: 1031

Answers (3)

John Slavick
John Slavick

Reputation: 10619

Your solution can be greatly simplified since the release of Docker 18.03.

The host.docker.internal DNS name has been made available to running docker containers. These DNS entries hold the IP address of the docker host.

So instead of xdebug.remote_host=172.17.0.1 you can do xdebug.remote_host=host.docker.internal

You can also remove the part of the run script that runs the docker container merely to get the host's IP address.

Upvotes: 1

bpds
bpds

Reputation: 672

I've finally found the solution.It was not so complex. I ve used 'telnet' to determine if the container was able to reach the host on the 9000. And it was not. The confusion was coming from the remote host IP. You must use your localhost IP!!!

HOST_IP=$(ifconfig en0 | grep inet | grep -v inet6 | awk '{print $2}')

docker run  -e PHP_IDE_CONFIG="serverName=DockerLocal"\
                -e XDEBUG_CONFIG="idekey=PHPSTORM"\
                -e XDEBUG_CONFIG="remote_host=$HOST_IP"\
                -p 27017:27017 \
                -p 8001:80\
                -d --name DockerizedSample dockerizedsampleimage

Check details on hub.docker.com Or directly on the github repository of Bartleby's Php-Apache-Mongo

Upvotes: 0

Nikita Nikitin
Nikita Nikitin

Reputation: 1

Basically for the remote debugging the extension does nothing, but setting the cookie XDEBUG_SESSION to a value of an idekey. In your "complicated" case I'd better check the following 4 things, that came into my mind first:

  1. Your xdebug.remote_port=9000.

If the port #9000 is occupied on the server side (the side, where you use your IDE) by php-fpm, the IDE can not start listening incoming connections. Simply 9000 is the default port for fpm, and this often becomes a common mistake. And that's why I use the port #10000.

  1. docker run -e PHP_IDE_CONFIG="serverName=Dockerized"

PHP_IDE_CONFIG here is not an environment variable. This is actually a parameter of the fastcgi_param directive of an nginx config.

  1. Use xdebug.remote_log=/path/to/xdebug_remote.log

  2. Personally I prefer to use old software. So I'm not a good helper here, but as far as I heard Phpstorm now uses a different configuration for remote debugging.

Upvotes: 0

Related Questions