简体   繁体   English

在 Mac OS 上的 Dockerized PHP 中使用 XDebug

[英]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.我无法从 macOS 上的 Dockerized 容器中使用 Xdebug 进行调试。

I ve read very carefully : XDebug Remote configuration , Nikita Nikitin post And all the proposed solutions on Stackoverflow and Github.我已经非常仔细地阅读了: XDebug 远程配置Nikita Nikitin 帖子以及 Stackoverflow 和 Github 上的所有建议解决方案。 I'm still blocked..我还是被屏蔽了。。

In the container在容器中

I can connect to the container bash -c "clear && docker exec -it DockerizedSample sh"我可以连接到容器bash -c "clear && docker exec -it DockerizedSample sh"

It confirms that XDebug is installed.它确认安装了 XDebug。

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

After building the image (may take a few minutes) and running the container it should open a browser on http://localhost:8001/构建映像(可能需要几分钟)并运行容器后,它应该在http://localhost:8001/上打开浏览器

My current testing environment我目前的测试环境

  • PhpStorm 2016.2.1 PhpStorm 2016.2.1
  • Build #PS-162.1889.1, built on August 23, 2016构建 #PS-162.1889.1,构建于 2016 年 8 月 23 日
  • You have perpetual fallback license for this version您拥有此版本的永久后备许可证
  • JRE: 1.8.0_76-release-b216 x86_64 JRE:1.8.0_76-release-b216 x86_64
  • JVM: OpenJDK 64-Bit Server VM by JetBrains sro JVM:JetBrains sro 的 OpenJDK 64 位服务器 VM
  • macOS Sierra 10.12 (16A323) macOS Sierra 10.12 (16A323)
  • Docker for mac Installed from the official docker for mac Version 1.12.1 (build: 12133) 2d5b4d9c3daa089e3869e6355a47dd96dbf39856 Docker for mac 从官方docker for mac版本 1.12.1 (build: 12133) 2d5b4d9c3daa089e3869e6355a47dd96dbf39856 安装

Your solution can be greatly simplified since the release of Docker 18.03.自 Docker 18.03 发布以来,您的解决方案可以大大简化。

The host.docker.internal DNS name has been made available to running docker containers. host.docker.internal DNS 名称已可用于运行host.docker.internal容器。 These DNS entries hold the IP address of the docker host.这些 DNS 条目包含 docker 主机的 IP 地址。

So instead of xdebug.remote_host=172.17.0.1 you can do xdebug.remote_host=host.docker.internal所以代替xdebug.remote_host=172.17.0.1你可以做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.您还可以删除运行 docker 容器的 run 脚本部分,只是为了获取主机的 IP 地址。

Basically for the remote debugging the extension does nothing, but setting the cookie XDEBUG_SESSION to a value of an idekey.基本上对于远程调试,扩展什么都不做,而是将 cookie XDEBUG_SESSION 设置为 idekey 的值。 In your "complicated" case I'd better check the following 4 things, that came into my mind first:在您的“复杂”情况下,我最好检查以下 4 件事,这是我首先想到的:

  1. Your xdebug.remote_port=9000.你的 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.如果 php-fpm 在服务器端(使用 IDE 的那一侧)端口 #9000 被 php-fpm 占用,则 IDE 将无法开始侦听传入连接。 Simply 9000 is the default port for fpm, and this often becomes a common mistake. 9000 是 fpm 的默认端口,这经常成为一个常见的错误。 And that's why I use the port #10000.这就是我使用端口 #10000 的原因。

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

PHP_IDE_CONFIG here is not an environment variable . PHP_IDE_CONFIG 在这里不是环境变量 This is actually a parameter of the fastcgi_param directive of an nginx config.这实际上是 nginx 配置的fastcgi_param指令的参数。

  1. Use xdebug.remote_log=/path/to/xdebug_remote.log使用 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.所以我在这里不是一个好帮手,但据我所知 Phpstorm 现在使用不同的配置进行远程调试。

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.我已经使用“telnet”来确定容器是否能够访问 9000 上的主机。但事实并非如此。 The confusion was coming from the remote host IP.混乱来自远程主机 IP。 You must use your localhost IP!!!您必须使用您的本地主机 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查看hub.docker.com详细信息或直接在Bartleby 的 Php-Apache-Mongo 的 github 存储库上查看

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM