Configure remote debugging with XDebug for php docker container on macOS

Yulia Kostrikova 🇺🇦❤️
3 min readSep 16, 2017

--

After moving from linux to macOS I bumped into looping back to host issue…

https://xdebug.org/docs/remote

Pre-conditions
macOS Sierra
Docker CE Version 17.06.2-ce-mac27
PHP 7.1.8
XDebug v2.5.0
PhpStorm 2017.2

If you want to set up remote debug from your container you have 2 options:
1. xdebug.remote_host = %host_ip%
%host_ip% — The IP of the machine running your IDE. It’s assumed on the same host as Docker.
OR
2.xdebug.remote_connect_back = on
It checks the $_SERVER[‘HTTP_X_FORWARDED_FOR’] and $_SERVER[‘REMOTE_ADDR’] variables to find out which IP address to use.

You cannot use xdebug.remote_connect_back because it takes containers’s internal IP. So xdebug.remote_host is the only option we have.
It’s pretty simple to get host IP on Linux OS just look at : ifconfig > docker0. Moreover you can create a fixed set of IPs for both host and containers using docker network.

For instance:
docker network create -d bridge — subnet 192.168.0.0/24 — gateway 192.168.0.1 dockernet
Now each container can connect to the host under the fixed IP 192.168.0.1.

But the docker0 interface is not available for Docker in macOS. This interface is actually within HyperKit. So unfortunately there is no a reliable way of looping back to host for macOS. The only solution is to create your local IP’s alias to something else : sudo ifconfig lo0 alias 10.254.254.254

And now the complete guide:
1. sudo ifconfig lo0 alias 10.254.254.254

For permanent setting up save.plist file
sudo curl -o /Library/LaunchDaemons/com.yuklia.docker_localhost_alias.plist https://gist.github.com/yuklia/378a7350f8a2456e9513154b962c7cb0

here it is

2. build docker containers
3. add server to IDE

4. configure Debug’s port in my case it’s 9002. xDebug client will knock to this port.

5. configure DBGp Proxy

Docker file based on php:7-fpm with XDebug

Php service for docker-compose.yml

Btw if you need to get a shell of the VM that was created by HyperKit run:
screen ~/Library/Containers/com.docker.docker/Data/com.docker.driver.amd64-linux/tty

or

screen ~/Library/Containers/com.docker.docker/Data/vms/0/tty

Once you get the tty running you can navigate to /var/lib/docker

For getting all screens run: screen -ls
For exiting screen run : screen -X -S %screen_name% kill

So that’s all for now. Hope it will save a bunch of time for someone! I will be glad to get comments and questions. Happy hacking=)

--

--