6

I am trying to debug Laravel code on Visual Studio Code using Xdebug. I am using Docker container that works as server.

But I am getting this error when try to debug:

Cannot connect to runtime process, timeout after 10000 ms - (reason: Cannot connect to the target: connect ECONNREFUSED 192.168.99.100:9000).

Here is my VSCode launch.json

    "version": "0.2.0",
    "configurations": [

        {
            "type": "node",
            "request": "attach",
            "name": "Docker: Attach to Node",
            "port": 9000,
            "address": "192.168.99.100",
            "localRoot": "${workspaceFolder}",
            "remoteRoot": "/var/www",
            "protocol": "inspector",
            "restart": true
        }
]

Here is docker-compose.yml

  app:
    build:
      context: ./
      dockerfile: .docker/app.dockerfile
      args:
      - INSTALL_XDEBUG=true
    working_dir: /var/www
    volumes:
      - ./:/var/www
    environment:
      - "DB_PORT=33061"
      - "DB_HOST=192.168.99.100"
      - XDEBUG_CONFIG=remote_host=192.168.99.100 remote_port=9000 remote_connect_back=0

Here is my app.dockerfile

FROM php:7.1-fpm

RUN docker-php-ext-install pdo &&  docker-php-ext-install pdo_mysql

#####################################
# xDebug:
#####################################

ARG INSTALL_XDEBUG=true
RUN if [ ${INSTALL_XDEBUG} = true ]; then \
    # Install the xdebug extension
    pecl install xdebug && \
    docker-php-ext-enable xdebug \
;fi

# Copy xdebug configration for remote debugging
COPY .docker/xdebug.ini /usr/local/etc/php/conf.d/xdebug.ini


#####################################
# ZipArchive:
#####################################

ARG INSTALL_ZIP_ARCHIVE=false
RUN if [ ${INSTALL_ZIP_ARCHIVE} = true ]; then \
    # Install the zip extension
    docker-php-ext-install zip \
;fi

# RUN apt-get update && apt-get install -y \
#         freetds-bin \
#         freetds-dev \
#         freetds-common \
#         libct4 \
#     && docker-php-ext-install pdo_dblib

# pdo_dblib
RUN apt-get update && apt-get install -y freetds-dev
RUN docker-php-ext-configure pdo_dblib --with-libdir=/lib/x86_64-linux-gnu
RUN docker-php-ext-install pdo_dblib
RUN sed -i "s|\[sqlsrv\]|\[sqlsrv\]\nhost = 172.18.250.57\nport =1435\nclient charset=UTF-8\ntds version = 8.0|g" /etc/freetds/freetds.conf




RUN usermod -u 1000 www-data

WORKDIR /var/www

CMD ["php-fpm"]

EXPOSE 9000
EXPOSE 9001

I think VSCode can't connect to remote Xdebug with 9000 port. But when check to app docker image Xdebug is working properly. Maybe VSCode needs some more configuration. But I couldn't solve this issue.

LazyOne
  • 158,824
  • 45
  • 388
  • 391
tcetin
  • 979
  • 1
  • 21
  • 48
  • *"I think VSCode cant connect to remote Xdebug with 9000 port"* Please , start with reading [Xdebug official doc](https://xdebug.org/docs/remote) to have basic understanding of how it works before proceeding with configuration. VSCode does NOT connect to Xdebug! **It's other way around** -- Xdebug connects to client (VSCode in your case). So the IP address must be the IP where VSCode is running (as seen from container) and there is no need to expose 9000 port on container. – LazyOne Jul 04 '18 at 20:13
  • Do you have any solution about VSCode configuration? As you can see at the codes I did what you said. – tcetin Jul 05 '18 at 08:09
  • Generally speaking: 1) what is `192.168.99.100`? Is this a container IP or host machine? It has to be the host .. as it's xdebug that connects to VSCode 2) Do not expose 9000 port. This prevents VSCode from listening for incoming xdebug connections .. as port is occupied by Docker. 3) Get some manual specific to Docker + VSCode + Xdebug and follow it -- proper manual should have all steps listed and explained. – LazyOne Jul 05 '18 at 17:01

2 Answers2

5

You can use PHP Debug VS Code extension to connect with XDebug. PHP-FPM are run at port 9000 by default, so it's best to change the xdebug.remote_port settings to another port (e.g. 9001):

// launch.json

"version": "0.2.0",
"configurations": [
  {
    "name": "Listen for XDebug",
    "type": "php",
    "request": "launch",
    "pathMappings": { "/": "${workspaceRoot}/" },
    "port": 9001
  },
  {
    "name": "Launch currently open script",
    "type": "php",
    "request": "launch",
    "program": "${file}",
    "cwd": "${fileDirname}",
    "port": 9001
  }
]

If you are using Docker, Use these settings in your php.ini file :

//php.ini

[XDebug]
xdebug.remote_enable = 1
xdebug.remote_autostart = 1
xdebug.remote_host = host.docker.internal
xdebug.remote_port = 9001

As of Docker version 18.03, host.docker.internal points to host IP address on Mac / Windows. For Linux, you can use this workaround.

Once these settings are set, you are good to go.

danilo
  • 7,680
  • 7
  • 43
  • 46
Anugerah Erlaut
  • 990
  • 13
  • 23
  • I found a resolution, it's need to add a mapping to vscode link files and code: ```json "pathMappings": { "/": "${workspaceRoot}/" }``` https://stackoverflow.com/questions/60387471/debug-php-using-docker-in-vscode – danilo Feb 29 '20 at 23:54
0

I think you need to alter several things.

First the environmental variables in your docker-compose.yml (assuming you are using Xdebug 3 and higher)

    environment:
      XDEBUG_MODE: debug
      XDEBUG_CONFIG: client_host=host.docker.internal

And launch.json also appears incorrect. Change it to:

{
"name": "Listen for Xdebug",
"type": "php",
"request": "launch",
"port": 9003,
"pathMappings": {
"/var/www": "${workspaceFolder}"
},

But I would highly recommend just use Laravel Sail to set this up. It should be much more straightforward. Please check the tutorial I've put together on this topic: https://blog.devsense.com/2022/laravel-on-docker

Hopefully, it will help