yiisoft / yii2-docker

Official Docker images suitable for Yii 2.0
https://www.yiiframework.com/
BSD 3-Clause "New" or "Revised" License
381 stars 202 forks source link

PHP hangs using Xdebug #122

Closed cccaballero closed 3 years ago

cccaballero commented 3 years ago

I am using the yii2 advanced template and yiisoftware/yii2-php:7.4-apache image. When I setup my docker compose as:

services:
  frontend:
    build: frontend
    ports:
      - 20080:80
    volumes:
      # Re-use local composer cache via host-volume
      - ~/.composer-docker/cache:/root/.composer/cache:delegated
      # Mount source-code for development
      - ./:/app
    environment:
      PHP_ENABLE_XDEBUG: 1
    extra_hosts:
      - "host.docker.internal:172.17.0.1"

PHP hangs like forever (no matter if is running from CLI or browser). As I am running from linux, if I do not include the extra_hosts it does not hangs, but throws the error:

Xdebug: [Step Debug] Could not connect to debugging client. Tried: host.docker.internal:9005 (through xdebug.client_host/xdebug.client_port) :-(

I am using PhpStorm as IDE and configured the Xdebug port to 9005.

schmunk42 commented 3 years ago

How does your Docker setup look like (Docker Desktop, VM,...)? You need to connect to your host from the container which can be tricky.

Did you make sure 172.17.0.1 is always your host IP in the container?

cccaballero commented 3 years ago

@schmunk42 thanks for your answer.

I am using Ubuntu 20.04 and the official docker package for Ubuntu linux distribution. If I use extra_hosts I can reach my host using host.docker.internal. With and without extra_hosts I can reach it in the 172.17.0.1 IP. Checked and confirmed.

schmunk42 commented 3 years ago

Could you try some steps from here: https://github.com/dmstr/phd5-docs/blob/master/guide/tutorials/xdebug.md

I have a similar setup (Docker native on a Arch Linux host) and I remember that I always missed the firewall part :)

For debugging, I'd recommend installing telnet in a container to really check if you are able to connect.

cccaballero commented 3 years ago

@schmunk42 I got it working now, but I am not sure why... the only different thing is that, when I started today the container it throws an error because the port 20080 was already in use, I checked in the PhpStorm debug options and there was in the "Zend Debugger" section an option for "Settings broadcasting port" using 20080 as port (the same as the app port mapping). I changed it to 20081 and now the debugging is working fine. Maybe that was the cause of the hang, some kind of weird port collision.

schmunk42 commented 3 years ago

Good to hear it's working, I also saw similar things.

cccaballero commented 3 years ago

@schmunk42 Thanks a lot for your time and help!!

mikeserov commented 3 years ago

@schmunk42 I got it working now, but I am not sure why... the only different thing is that, when I started today the container it throws an error because the port 20080 was already in use, I checked in the PhpStorm debug options and there was in the "Zend Debugger" section an option for "Settings broadcasting port" using 20080 as port (the same as the app port mapping). I changed it to 20081 and now the debugging is working fine. Maybe that was the cause of the hang, some kind of weird port collision.

Thank you very much! It also works for me. I had the same problem, and tried to solved it all evening long. I read multiple topics on forums, guide on jetBrains, and other stuff. I thought I checked all possible variants, and I had started loosing hope, but your desicion helped me. Thanks!
Снимок экрана от 2021-07-04 00-27-51 Снимок экрана от 2021-07-04 00-28-10 Снимок экрана от 2021-07-04 00-28-44