沃梦达 / 编程问答 / php问题 / 正文

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

Geting the private ip for the docker network from within the container to configure xdebug remote_host(从容器内获取 docker 网络的私有 ip 以配置 xdebug remote_host)

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

问题描述

所以使用 docker 和 docker-compose 我喜欢提供内置到容器中的工具,这样开发团队中的其他人就不必为设置零碎而烦恼.在这种特定情况下,我在配置 xdebug 时遇到了问题.从浏览器调试时,使用回连功能绝对正常.但是试图让 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.

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:从容器内获取 docker 网络的私有 ip 以配置 xdebug remote_host

基础教程推荐