从容器中获取Docker网络的私有IP以配置xdebug remote_host [英] Geting the private ip for the docker network from within the container to configure xdebug remote_host

查看:187
本文介绍了从容器中获取Docker网络的私有IP以配置xdebug remote_host的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我想使用docker和docker-compose来提供内置于容器中的工具,以便开发团队中的其他人不必为设置位和鲍勃而费力。在这种情况下,我在配置xdebug时遇到问题。从浏览器调试时,使用connect back可以正常工作。但是,试图让xdebug通过容器内部的cli工作绝对是一场噩梦。由于某种原因,它需要(与remote_autostart = 1一起)remote_host设置指向Docker容器所在的网络。

So using docker and docker-compose I like to provide tools built into the containers so that other people in the development team don't have to struggle with setting up bits and bobs. In this specific case I'm having issues configuring xdebug. It works absolutely fine using connect back when debugging from a browser. But trying to get xdebug working through the cli inside the container is being an absolute nightmare. For some reason it requires (along with remote_autostart=1) the remote_host setting to point to the network the docker container is in.

这是我当前的解决方案〜/ .bashrc,它可以工作,但是太可怕了。它的工作原理是假设网络ip只是容器ip,但最后一位被1代替。我希望有人有更好的方法来使xdebug正常工作,或者有更好的方法来获取网络ip。 / p>

This is my current solution which is in my ~/.bashrc, and it works, but it's horrible. It works off of the assumption that the network ip will just be the containers ip but the last digit is replaced with a 1. I'm hoping someone has a better way to get xdebug working or a nicer way to fetch the network ip.

# 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 来获取它:

The host can be accessed from within the container using the IP address of the default gateway (that is the IP of the docker0 network interface on host). Use ip to get it:

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

这篇关于从容器中获取Docker网络的私有IP以配置xdebug remote_host的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆