Skip to content
Advertisement

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

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.

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 settings:

[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/

Advertisement

Answer

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}'
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement