在Mac OS上的Dockerized PHP中使用XDebug

问题描述 投票:2回答:3

The issue

我无法在macOS上使用Dockerized容器中的Xdebug进行调试。

我仔细阅读过:XDebug Remote configuration Nikita Nikitin post以及Stackoverflow和Github上提出的所有解决方案。我还是被封了..

在容器中

我可以连接到容器bash -c "clear && docker exec -it DockerizedSample sh"

它确认已安装XDebug。

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

[Zend Modules]
Xdebug

它的配置似乎有效。

# 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. 下载Php-Apache-Mongo/zip/master
  2. 转到PHPStorm文件夹并运行shell脚本./run.sh

构建映像(可能需要几分钟)并运行容器后,它应该在http://localhost:8001/上打开一个浏览器

My current testing environment

  • PhpStorm 2016.2.1
  • Build#PS-162.1889.1,建于2016年8月23日
  • 您拥有此版本的永久备用许可证
  • JRE:1.8.0_76-release-b216 x86_64
  • JVM:JetBrains s.r.o的OpenJDK 64位服务器VM
  • macOS Sierra 10.12(16A323)
  • Docker for mac安装于官方docker for mac版本1.12.1(版本:12133)2d5b4d9c3daa089e3869e6355a47dd96dbf39856
php shell docker xdebug
3个回答
1
投票

自Docker 18.03发布以来,您的解决方案可以大大简化。

host.docker.internal DNS名称已用于运行docker容器。这些DNS条目包含docker主机的IP地址。

因此,你可以做xdebug.remote_host=172.17.0.1而不是xdebug.remote_host=host.docker.internal

您还可以删除运行docker容器的运行脚本部分,以获取主机的IP地址。


0
投票

基本上对于远程调试,扩展不执行任何操作,但将cookie XDEBUG_SESSION设置为idekey的值。在你的“复杂”案例中,我最好先检查以下4件事情,我首先想到的是:

  1. 你的xdebug.remote_port = 9000。

如果通过php-fpm在服务器端(您使用IDE的那一侧)占用端口#9000,则IDE无法开始侦听传入连接。简单地说9000是fpm的默认端口,这常常是一个常见的错误。这就是我使用端口#10000的原因。

  1. docker run -e PHP_IDE_CONFIG =“serverName = Dockerized”

这里的PHP_IDE_CONFIG不是environment variable。这实际上是nginx配置的fastcgi_param指令的参数。

  1. 使用xdebug.remote_log = / path / to / xdebug_remote.log
  2. 我个人更喜欢使用旧软件。所以我在这里不是一个好帮手,但据我所知,Phpstorm现在使用不同的配置进行远程调试。

0
投票

我终于找到了解决方案。它并不那么复杂。我使用'telnet'来确定容器是否能够到达9000上的主机。事实并非如此。混乱来自远程主机IP。你必须使用你的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

检查details on hub.docker.com或直接在the github repository of Bartleby's Php-Apache-Mongo

© www.soinside.com 2019 - 2024. All rights reserved.