繁体   English   中英

从容器内获取 docker 网络的私有 ip 以配置 xdebug remote_host

[英]Geting the private ip for the docker network from within the container to configure xdebug remote_host

因此,使用 docker 和 docker-compose 时,我喜欢提供内置于容器中的工具,这样开发团队中的其他人就不必为设置小工具而苦恼。 在这种特定情况下,我在配置 xdebug 时遇到问题。 从浏览器调试时,使用 connect back 绝对可以正常工作。 但是试图让 xdebug 通过容器内的 cli 工作绝对是一场噩梦。 由于某种原因,它需要(连同 remote_autostart=1)remote_host 设置指向 docker 容器所在的网络。

这是我当前的解决方案,在我的 ~/.bashrc 中,它可以工作,但很糟糕。 它假设网络 ip 将只是容器 ip 但最后一个数字被替换为 1。我希望有人有更好的方法来让 xdebug 工作或更好的方法来获取网络 Z94535B527BCFFAD2318

# If xdebug doesn't get provided a remote address then it will default to this. This is the case for cli debugging. 
# This ip should be the network this container is running on
own_private=$(hostname -i | awk '{print $1}')
# Replaces the final number from the containers own private ip with a 1 for the network address
network="${own_private%.*}.1"
# For some reason xdebug won't work unless you give it the network ip
export XDEBUG_CONFIG="remote_host=$network"

Xdebug 设置:

[xdebug]
zend_extension=xdebug.so
xdebug.remote_enable = 1
xdebug.remote_connect_back = 1
xdebug.remote_port = 9000
xdebug.show_local_vars = 0
xdebug.var_display_max_data = 10000
xdebug.var_display_max_depth = 20
xdebug.show_exception_trace = 0
xdebug.remote_autostart=1
xdebug.idekey = "PHPSTORM"
xdebug.remote_log = /srv/www/var/log/xdebug.log
xdebug.profiler_enable = 0;
xdebug.profiler_enable_trigger = 1;
xdebug.profiler_output_dir = /srv/www/var/profiler/

可以使用默认网关的 IP 地址从容器内访问主机(即主机上docker0网络接口的 IP)。 使用ip得到它:

ip route show default | awk '/default/ {print $3}'

暂无
暂无

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

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