skip to Main Content

PHP version: 7.1.20

XDebug version: 2.7.0

I’m using a Docker container on MacOS Mojave.
On pressing “F5”, it shows debug options Pause, Restart, and Stop. But Step Over, Step Into, Step Out are disabled.

I’m kind of learner because I was used to code till 2012-13 on Windows OS. After that year, I am back to coding this month. 🙂 Even after reviewing a lot of posts on Google about how to resolve this issue, I’m not sure how to finally make it work. Please help.

My launch.json file:

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

XDebug php.ini config:

xdebug.remote_host = 172.20.0.1
xdebug.remote_port = 9001
xdebug.scream = 1
xdebug.remote_enable = 1
xdebug.show_local_vars = 1
xdebug.remote_autostart = 1
xdebug.remote_connect_back = 1
xdebug.remote_log = "/var/www/html/xdebug.log"
xdebug.idekey = "VSCODE"

XDebug logfile (from setting xdebug.remote_log in php.ini):

[12] Log opened at 2019-05-12 10:16:44
[12] I: Checking remote connect back address.
[12] I: Checking header 'HTTP_X_FORWARDED_FOR'.
[12] I: Checking header 'REMOTE_ADDR'.
[12] I: Remote address found, connecting to 172.20.0.1:9001.
[12] W: Creating socket for '172.20.0.1:9001', poll success, but error: Operation now in progress (29).
[12] E: Could not connect to client. :-(
[12] Log closed at 2019-05-12 10:16:44

Debug console:

<- launchResponse
Response {
  seq: 0,
  type: 'response',
  request_seq: 2,
  command: 'launch',
  success: true }

Here’s the dockerfile.

FROM php:7.1.20-apache

RUN apt-get -y update --fix-missing
RUN apt-get upgrade -y

# Install tools & libraries
RUN apt-get -y install apt-utils nano wget dialog 
    build-essential git curl libcurl3 libcurl3-dev zip

# Install important libraries
RUN apt-get -y install --fix-missing apt-utils build-essential git curl libcurl3 libcurl3-dev zip 
    libmcrypt-dev libsqlite3-dev libsqlite3-0 mysql-client zlib1g-dev 
    libicu-dev libfreetype6-dev libjpeg62-turbo-dev libpng-dev

# Composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

# PHP Extensions
RUN pecl install xdebug-2.7.0 
    && docker-php-ext-enable xdebug 
    && docker-php-ext-install mcrypt 
    && docker-php-ext-install pdo_mysql 
    && docker-php-ext-install pdo_sqlite 
    && docker-php-ext-install mysqli 
    && docker-php-ext-install curl 
    && docker-php-ext-install tokenizer 
    && docker-php-ext-install json 
    && docker-php-ext-install zip 
    && docker-php-ext-install -j$(nproc) intl 
    && docker-php-ext-install mbstring 
    && docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ 
    && docker-php-ext-install -j$(nproc) gd 
    && pecl install redis 
    && docker-php-ext-enable redis

# Enable apache modules
RUN a2enmod rewrite headers

ENTRYPOINT ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]

Here’s the docker-compose.yml file.

version: "3"

services:
  webserver:
    build: 
      context: ./bin/webserver
    container_name: '7.1.x-webserver'
    restart: 'always'
    ports:
      - "80:80"
      - "443:443"
    links: 
      - mysql
    volumes: 
      - ${DOCUMENT_ROOT-./www}:/var/www/html
      - ${PHP_INI-./config/php/php.ini}:/usr/local/etc/php/php.ini
      - ${VHOSTS_DIR-./config/vhosts}:/etc/apache2/sites-enabled
      - ${LOG_DIR-./logs/apache2}:/var/log/apache2
  mysql:
    build: ./bin/mysql
    container_name: '5.7-mysql'
    restart: 'always'
    ports:
      - "3306:3306"
    volumes: 
      - ${MYSQL_DATA_DIR-./data/mysql}:/var/lib/mysql
      - ${MYSQL_LOG_DIR-./logs/mysql}:/var/log/mysql
    environment:
      MYSQL_ROOT_PASSWORD: tiger
  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    container_name: 'sc-phpmyadmin'
    links:
      - mysql
    environment:
      PMA_HOST: mysql
      PMA_PORT: 3306
    ports:
      - '8080:80'
    volumes: 
      - /sessions
  redis:
    container_name: 'sc-redis'
    image: redis:latest
    ports:
      - "6379:6379"

Thank you in advance.

4

Answers


  1. Try to open port 9001 in the docker-compose.yml by inserting:

    ports:
     - "9001:9001"
    

    Example

    services:
      webserver:
        build: 
          context: ./bin/webserver
        container_name: '7.1.x-webserver'
        restart: 'always'
        ports:
          - "80:80"
          - "443:443"
          - "9001:9001"
        links: 
          - mysql
        volumes: 
          - ${DOCUMENT_ROOT-./www}:/var/www/html
          - ${PHP_INI-./config/php/php.ini}:/usr/local/etc/php/php.ini
          - ${VHOSTS_DIR-./config/vhosts}:/etc/apache2/sites-enabled
          - ${LOG_DIR-./logs/apache2}:/var/log/apache2
    

    See more about configuring docker-compose.yml: https://docs.docker.com/compose/compose-file/

    You might be able to use expose. We have that in some of our old Dockerfiles which is intended to allow xDebug connections, but I haven’t used it personally.

        expose:
          - "9001"
    
    Login or Signup to reply.
  2. You can have your docker container connect to your host by configuring PHP appropriately.

    Note that I’ve used Docker’s host.docker.internal domain, that always points to the host IP.

    xdebug.remote_enable=1
    xdebug.remote_port=9000
    xdebug.idekey=docker
    xdebug.profiler_enable=0
    xdebug.profiler_enable_trigger=1
    xdebug.remote_host=host.docker.internal
    

    There’s no need to open ports to the docker container.
    Just make sure that your debugging application can listen on the appointed port (9000 in my case) and that this port is not occupied.

    Take for example this PhpStorm configuration:
    Configuration

    Troubleshooting

    Validate that xdebug is installed, run in the php container:

    php -i | grep xdebug

    Make sure that you’re editing the appropriate .ini files, check:

    php -i | grep .ini

    If you have idekey enabled, make sure to configure them in your IDE too:

    config remote debug

    There’s a Debugger Config Validator in PhpStorm if you’re exposing PHP scripts on a webserver:

    Validate

    Login or Signup to reply.
    1. You should open port 9001 in your host machine, not in Docker.
    2. Also try to check if Xdebug is configured properly: add phpinfo() to your code and open that page in browser. You shold see xdebug enabled
    3. If Xdebug enabled, maybe your IDE settings are incorrect
    Login or Signup to reply.
  3. For VSCode as the OP has asked and xdebug 3.x.x, the configuration should be –

    xdebug.mode=debug
    xdebug.start_with_request=yes
    xdebug.start_upon_error=yes
    xdebug.client_host=[IP address] <<<< NOTE
    xdebug.discover_client_host=true
    xdebug.client_port=9000
    

    Note: xdebug.client_host should have the host IP address for vscode (192.168…) and not host.docker.internal which does not seem to work for vscode.

    Note 2: There is no need to open any port (9000 in the config above) for this purpose. This could be because docker connects back to host at port 9000 and not vice versa!

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