skip to Main Content

I have a Docker environment running.

To get Xdebug running, I followed the steps from this page:
https://devdocs.magento.com/cloud/docker/docker-development-debug.html

When I try to switch on "Start Listening for PHP Debug Connections", I get the error:

Can’t start listening for connections from ‘xdebug’: Port 9001 is busy

Does anybody know how to setup Magento in docker with Xdebug correctly?

EDIT:
The whole environment looks like this:

CONTAINER ID        IMAGE                                                COMMAND                  CREATED             STATUS                            PORTS                              NAMES
19d92752c73a        magento/magento-cloud-docker-tls:latest-1.1          "/entrypoint.sh"         4 seconds ago       Up 3 seconds                      0.0.0.0:443->443/tcp               mymagento_tls_1
b71dd39b46c3        magento/magento-cloud-docker-nginx:latest-1.1        "/docker-entrypoint.…"   4 seconds ago       Up 4 seconds (health: starting)   0.0.0.0:80->80/tcp, 443/tcp        mymagento_web_1
311e04f363b3        magento/magento-cloud-docker-php:7.3-fpm-1.1         "/docker-entrypoint.…"   5 seconds ago       Up 4 seconds (health: starting)   9000/tcp                           mymagento_fpm_1
cbdf9246f3e9        magento/magento-cloud-docker-php:7.3-fpm-1.1         "/docker-entrypoint.…"   35 seconds ago      Up 33 seconds (healthy)           9000/tcp, 0.0.0.0:9001->9001/tcp   mymagento_fpm_xdebug_1
2f1b4f2f9e23        magento/magento-cloud-docker-elasticsearch:6.5-1.1   "/usr/local/bin/dock…"   36 seconds ago      Up 35 seconds (healthy)           9200/tcp, 9300/tcp                 mymagento_elasticsearch_1
d818c0755f4c        mariadb:10.2                                         "docker-entrypoint.s…"   36 seconds ago      Up 35 seconds (healthy)           0.0.0.0:32807->3306/tcp            mymagento_db_1
19e266fbb72a        redis:5.0                                            "docker-entrypoint.s…"   36 seconds ago      Up 35 seconds (healthy)           0.0.0.0:32806->6379/tcp            mymagento_redis_1

3

Answers


  1. Chosen as BEST ANSWER

    The issue is fixed by adding the xdebug extension to the xdebug docker container config:

    fpm_xdebug:
      environment:
          - 'PHP_EXTENSIONS=bcmath bz2 calendar exif gd gettext intl mysqli pcntl pdo_mysql soap sockets sysvmsg sysvsem sysvshm opcache zip redis xsl blackfire sodium xdebug'
    

    And by using the Port 9002 in the php.ini and in Phpstorm (Thanks @LazyOne).


  2. Besides the port issue, my Linux machine can’t resolve host.docker.internal host, so I replaced it with IP-address 172.17.0.1
    (https://github.com/magento/devdocs/issues/7400).

    You can check what is yours IP-address using a command:

    $ docker run --rm alpine ip route | awk 'NR==1 {print $3}'
    

    To avoid losing configuration by rewriting docker-composer.yml and .docker/config.env file (when you’ll do next time ece-docker build:compose command), I decided to add new configuration to new docker-compose.override.yml file:

    version: '2.1'
        fpm_xdebug:
            environment:
                - 'PHP_EXTENSIONS=bcmath bz2 calendar exif gd gettext intl mysqli pcntl pdo_mysql soap sockets sysvmsg sysvsem sysvshm opcache zip redis xsl sodium xdebug'
                - 'XDEBUG_CONFIG=remote_host=172.17.0.1 remote_port=9002'
    

    Then, set a new value of Xdebug listening port at PhpStorm: 9002

    Login or Signup to reply.
    1. First of all remove comments from these lines in
      DockerFolder/bin/webserver/Dockerfile.yml

    RUN pecl install xdebug-2.9.6
    RUN docker-php-ext-enable xdebug

    here you can see I have set version to 2.9.6, you do the same

    1. Now add these lines in your Config/php/php.ini file

      xdebug.remote_host=172.17.0.1/16

      xdebug.default_enable=1

      xdebug.remote_autostart=1

      xdebug.remote_connect_back=1

      xdebug.remote_enable=1

      xdebug.remote_handler=dbgp

      xdebug.remote_port=9000

      xdebug.idekey=VSCODE

      xdebug.extended_info=1

    To find host mentioned in php.ini file changes first line
    xdebug.remote_host=172.17.0.1/16

    Run below command

       ip a
    

    you will find
    172.17.0.1/16
    or
    172.27.0.1/16
    something similar to it. Try them one by one.
    Php.ini file will automatically take those changes when you do

     sudo docker-compose down -v && sudo docker-compse up -d
    
    1. In your Vscode launch.json file add these configurations. These are the same as default configuration but just 3 new lines added.

      {
      "version": "0.2.0",
      "configurations": [
      {
      "name": "Listen for Xdebug",
      "type": "php",
      "request": "launch",
      "port": 9000,
      "pathMappings": {
      "/var/www/html/": "${workspaceFolder}",
      },
      },
      {
      "name": "Launch currently open script",
      "type": "php",
      "request": "launch",
      "program": "${file}",
      "cwd": "${fileDirname}",
      "port": 9000
      }
      ]
      }

    Those 3 new lines are

     "pathMappings": {
                        "/var/www/html/": "${workspaceFolder}",
                    },
    
    1. This video will help
      https://www.youtube.com/watch?v=mahIIF0c8Zo

    I have also saved a docker_image which will automatically install xdebug along with other magento environment. May be you had to find xdebug.remote_host=172.17.0.1/16 , as described in step 2.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search