Docker + Xdebug + VSCode无法连接到客户端

时间:2019-05-12 12:07:38

标签: php docker visual-studio-code docker-compose xdebug

PHP版本:7.1.20

XDebug版本:2.7.0

我在MacOS Mojave上使用Docker容器。 按下“ F5”时,它将显示调试选项“暂停”,“重新启动”和“停止”。但是“跨步”,“步入”,“步出”被禁用。

我是一个学习者,因为我习惯在Windows OS上进行编码直到2012-13年。那年之后,我本月回到编码。 :)即使查看过Google上有关如何解决此问题的许多帖子,我仍不确定如何最终使它生效。请帮忙。

我的launch.json文件:

{
    "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配置:

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日志文件(来自在php.ini中设置xdebug.remote_log):

[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

调试控制台:

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

这里是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"]

这是docker-compose.yml文件。

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"

谢谢。

4 个答案:

答案 0 :(得分:1)

尝试通过插入以下内容在9001中暴露端口docker-compose.yml

expose:
 - "9001"

示例

services:
  webserver:
    build: 
      context: ./bin/webserver
    container_name: '7.1.x-webserver'
    restart: 'always'
    ports:
      - "80:80"
      - "443:443"
    expose:
      - "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

查看有关配置docker-compose.yml的更多信息:https://docs.docker.com/compose/compose-file/

答案 1 :(得分:0)

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

答案 2 :(得分:0)

  1. 您应该在主机而不是Docker中打开端口9001。
  2. 还尝试检查Xdebug的配置是否正确:将phpinfo()添加到代码中,然后在浏览器中打开该页面。您应该看到启用了xdebug
  3. 如果启用了Xdebug,则可能是您的IDE设置不正确

答案 3 :(得分:0)

对于 OP 要求的 VSCode 和 xdebug 3.x.x,配置应该是 -

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

注意: xdebug.client_host 应该有 vscode 的主机 IP 地址 (192.168...) 而不是 host.docker.internal,这似乎不适用于 vscode。

注意 2: 不需要为此打开任何端口(上面配置中的 9000)。这可能是因为 docker 连接回主机的 9000 端口,反之亦然!