skip to Main Content

I’m trying to use xdebug 3 with docker on ubuntu 20.04, but I’m not getting success,
xdebug does not enter the interruption point, I already searched for everything and no answer solved my problem, it would be something about the docker host, because the same configuration is the right one in windows, I don’t know what else I can try to solve the problem,
I would like a help to understand what I’m doing wrong, below is my configuration.

My docker-compose file

version: '3.7'

networks:
  supervisao:

services:
  nginx:
    image: nginx:stable-alpine
    container_name: supervisao-web
    ports:
      - "80:80"
    volumes:
      - .:/var/www/html/
      - ./.docker/web/default.conf:/etc/nginx/conf.d/default.conf
    depends_on:
      - php
      - mysql
    networks:
      - supervisao
  mysql:
    image: mysql:latest
    container_name: supervisao-db
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    tty: true
    ports:
      - "3306:3306"
    volumes:
      - ./.docker/mysql/:/var/lib/mysql
    environment:
      MYSQL_DATABASE: supervisao
      MYSQL_USER: user
      MYSQL_PASSWORD: pass
      MYSQL_ROOT_PASSWORD: pass
      SERVICES_TAGS: dev
      SERVICES_NAME: mysql
    networks:
      - supervisao
  php:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: supervisao-php
    volumes:
      - .:/var/www/html/
      - ./.docker/php/docker-xdebug.ini:/usr/local/etc/php/conf.d/php-docker.ini
    ports:
      - "9000:9000"
    networks:
     - supervisao
  redis:
    image: redis:latest
    volumes:
      - ./.docker/redis:/data
    ports:
      - 6379:6379
    networks:
      - supervisao

my xdebug.ini

# File: docker-xdebug.ini
zend_extension=/usr/local/lib/php/extensions/no-debug-non-zts-20190902/xdebug.so
xdebug.discover_client_host=1
xdebug.mode = debug
xdebug.start_with_request = yes
xdebug.client_host = host.docker.internal
xdebug.client_port = 9003
xdebug.log = /var/www/html/xdebug.log

I appreciate if someone can collaborate, thank you

2

Answers


  1. Make sure you have xdebug 3 loaded in PHP.
    Create the phpinfo.php file

    <?php
        phpinfo();
    

    Surf to it to check that xdebug is loaded by PHP and is version 3.

    If the xdebug is version 2.x then you can install v3 with pecl instead.
    If you have shell access to your docker box you can try the pecl command directly and see in phpinfo if you got xdebug v3.

    In your DockerFile:

    # PECL
    RUN mkdir -p /tmp/pear/cache
    RUN pecl channel-update pecl.php.net
    RUN apt install -y php-pear
    
    COPY xdebug.ini "/etc/php/${PHP_VERSION}/mods-available/xdebug.ini"
    # The xdebug distributed with Ubuntu 20.04 LTS is v2.9.2, we want v3.0.x
    RUN pecl install xdebug
    # Enable xdebug by default
    RUN phpenmod xdebug
     
    

    In your xdebug.ini

    zend_extension=xdebug.so
    xdebug.default_enable = On
    xdebug.mode=debug
    xdebug.start_with_request=yes
    xdebug.discover_client_host=yes
    xdebug.max_nesting_level = -1
    xdebug.log = "/var/www/log/xdebug.log"
    xdebug.output_dir = "/var/www/log/profiler" 
    

    Now you could rebuild the docker boxes

    docker stop  
    docker build --no-cache
    docker up
    

    Check in phpinfo if you got the settings enabled.

    If you have shell access to your docker box you can look at the log file.

    tail -n 100 /var/www/log/xdebug.log
    

    If you run PHP Storm there is an excellent validator in PHP Storm >> File >> Settings >> Languages & Frameworks >> PHP >> Debug
    Click the link "Validate". Set local web server path to your public folder. Validation script to your webb address. Works good on shared folders and not so good on synced folders.

    If you run Ubuntu as host then look at the firewall settings.
    "9000/tcp ALLOW IN Anywhere" and the same for port 9003. Or try temprarily to disable the firewall.

    I also added port 9003:9003 to the DockerFile. But have not tested if that makes any difference.

    Hope any of this was of help to you.

    Login or Signup to reply.
  2. In my case, disabling firewall did the trick – sudo ufw disable

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