4

I have few docker containers: php, nginx, mariadb, redis, adminer and try to debug some PHP code with PhpStorm and Xdebug.

Host (Lenovo T490 laptop):

$ hostname
T490


$ docker -v
Docker version 20.10.5, build 55c4c88


$ docker-compose -v
docker-compose version 1.24.1, build 4667896b


$ cat /etc/lsb-release 
DISTRIB_ID=neon
DISTRIB_RELEASE=20.04
DISTRIB_CODENAME=focal
DISTRIB_DESCRIPTION="KDE neon User Edition 5.21"


$ php -v
PHP 7.4.16 (cli) (built: Mar  5 2021 07:54:20) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.16, Copyright (c), by Zend Technologies


$ lsof -i :9003 | grep LISTEN
java    14868 kane   48u  IPv6 401093      0t0  TCP *:9003 (LISTEN)


$ cat /etc/hosts
127.0.0.1       localhost
127.0.1.1       T490

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

172.17.0.1 foo.pl adm.foo.pl api.foo.pl

PhpStorm 2020.3.3 config:

enter image description here

enter image description here

Docker-compose.yml

version: "3"
services:
  php-7.4:
    extra_hosts:
      - "host.docker.internal:host-gateway"
    build: ./docker-images/php-7.4
    image: ap/php:7.4
    container_name: foo-php-7.4
    environment:
      - GITHUB_API_TOKEN=${GITHUB_API_TOKEN}
      - XDEBUG_IDEKEY=${XDEBUG_IDEKEY}
    volumes:
      - ~/.composer-docker/cache:/root/.composer/cache:delegated
      - ~/.gitconfig:/root/.gitconfig
      - ${WORKSPACE_DIR}:/var/www
      - ~/.ssh:/root/ssh:ro
      - ./docker/php/conf.d/xdebug.ini:/usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini

XDebug in DockerFile install via:

RUN pecl install xdebug \
    && docker-php-ext-enable xdebug

.env

GITHUB_API_TOKEN=
WORKSPACE_DIR=/home/kane/workspace

XDEBUG_IDEKEY=PHPSTORM

COMPOSE_PROJECT_NAME=yii2fpm
COMPOSE_FILE=docker-compose.yml
X_LEGACY_GD_LIB=1
PHP_CGI_PASS=php-7.4:9000
NGINX_PORT=80
NGINX_SSL_PORT=443
DB_PORT=3306
ADMINER_PORT=8182

xdebug.ini

zend_extension=xdebug

[xdebug]
xdebug.mode=develop,debug
xdebug.client_host=172.17.0.1
xdebug.start_with_request = yes

After docker-compose up, in container:

# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         T490            0.0.0.0         UG    0      0        0 eth0
172.18.0.0      0.0.0.0         255.255.0.0     U     0      0        0 eth0


# ping T490
PING T490 (127.0.1.1) 56(84) bytes of data.
64 bytes from T490 (127.0.1.1): icmp_seq=1 ttl=64 time=0.057 ms


# ping 172.17.0.1
PING 172.17.0.1 (172.17.0.1) 56(84) bytes of data.
64 bytes from 172.17.0.1: icmp_seq=1 ttl=64 time=0.145 ms


# telnet 172.17.0.1 9003
Trying 172.17.0.1...


# telnet T490 9003
Trying 127.0.1.1...
telnet: Unable to connect to remote host: Connection refused


# php -v
PHP 7.4.16 (cli) (built: Mar 13 2021 02:52:33) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.16, Copyright (c), by Zend Technologies
    with Xdebug v3.0.3, Copyright (c) 2002-2021, by Derick Rethans

And when I go to http://adm.foo.pl/ (with cookie XDEBUG_SESSION:"PHPSTORM") docker log says:

NOTICE: PHP message: Xdebug: [Step Debug] Time-out connecting to debugging client, waited: 200 ms. Tried: 172.17.0.1:9003 (through xdebug.client_host/xdebug.client_port) :-(

After change xdebug.ini to:

zend_extension=xdebug

[xdebug]
xdebug.mode=develop,debug
xdebug.discover_client_host = yes
xdebug.start_with_request = yes

it says:

NOTICE: PHP message: Xdebug: [Step Debug] Could not connect to debugging client. Tried: 172.18.0.1:9003 (from REMOTE_ADDR HTTP header), localhost:9003 (fallback through xdebug.client_host/xdebug.client_port) :-(

after add extra_host says:

NOTICE: PHP message: Xdebug: [Step Debug] Time-out connecting to debugging client, waited: 200 ms. Tried: host.docker.internal:9003 (through xdebug.client_host/xdebug.client_port) :-(

Please, help :-)

TomaszKane
  • 805
  • 1
  • 11
  • 26

4 Answers4

5

How sad ... Usually I work on Centos7 so I looked for the firewall via firewall-cmd instead of ufw (on Ubuntu/Debian).

$ sudo ufw allow 9003

Now everything started working as it should.

TomaszKane
  • 805
  • 1
  • 11
  • 26
  • Just out of curiosity, does it also work with 172.17.0.1? – Eugene Morozov Mar 27 '21 at 14:04
  • Hm, in container `telnet 172.17.0.1 9003` works. XDebug log says `[Step Debug] INFO: Client host discovered through HTTP header, connecting to 172.18.0.1:9003. [34] [Step Debug] INFO: Connected to debugging client: 172.18.0.1:9003 (from REMOTE_ADDR HTTP header). :-)` – TomaszKane Mar 27 '21 at 14:22
  • 1
    Oh, that's `discover_client_host`'s doings, it can't work reliably in Docker. Then, your initial setup was correct too, apart from the `ufw` part. – Eugene Morozov Mar 27 '21 at 14:56
  • I'm getting a little similar issue, but I'm working on Windows. Any idea how to fix that? – techcase May 05 '21 at 20:24
4

With Docker on Linux, you can either

  1. Use the physical network interface (ens* or eth0 or something similar) IP to connect to the host, or
  2. Use this hack to be able to use host.docker.internal: https://github.com/docker/for-linux/issues/264#issuecomment-759737542

Installing telnet in the container to check the port's availability is always a good idea, the fact that 172.17.0.1 responds to ping doesn't necessarily mean that it's the host.

Eugene Morozov
  • 2,790
  • 1
  • 10
  • 17
  • Hi, I add `extra_hosts` to my php service in docker-compose, stop/start containers and install telent. No connection, why? – TomaszKane Mar 27 '21 at 13:01
1

After spending a couple of hours with this problem, I decided to talk to the 192.168.x.x IP address of the host, rather than the 172.17.0.1. It worked.

The exact value for x.x can be determined by these steps on Windows 10:

  • Start -> Command Prompt (Run as Administrator)
  • run the ipconfig | find "IPv4" command
  • use the value of 192.168....

This is how my xdebug config looks like now:

xdebug.client_host=192.168.1.110
xdebug.client_port="9003"
xdebug.start_upon_error=yes
xdebug.idekey = "PHPStorm"
xdebug.mode=develop,coverage,debug,profile
xdebug.discover_client_host=1
Csongor Halmai
  • 3,239
  • 29
  • 30
0

In my case firewall (ufw) on Ubuntu was not the cause: it was rootless Docker that prevented XDebug inside container from connecting to IDE on Host. XDebug needs a bridged network interface named br-xxxx and belonging to same network range as php container. This bridged interface can not be created while in rootless (so only Docker's docker0 is created). My other objective was to keep using host.docker.internal in compose.yml (instead of 192.168.x.x for ethernet or wifi).

Solution that worked for me:

  1. added to php service in compose.yml
extra_hosts:
      host.docker.internal: host-gateway
  1. added to docker-php-ext-xdebug.ini
xdebug.client_port=9003
xdebug.client_host=host.docker.internal
  1. running docker with sudo, e.g. sudo docker compose up -d

Taken from here: