如何在 php:7.4-fpm-alpine docker 容器中启用 xdebug?

How to enable xdebug in php:7.4-fpm-alpine docker container?

我的目标是将此 git 存储库用于 Laravel,并使用 xdebug 用于 php-fpm: https://github.com/aschmelyun/docker-compose-laravel

当我使用这个 repo 时 运行:

  1. docker-compose up -d --build site
  2. docker-整理

这是来自上面 repo 的 docker 文件:

FROM php:7.4-fpm-alpine

ADD ./php/www.conf /usr/local/etc/php-fpm.d/www.conf
RUN addgroup -g 1000 laravel && adduser -G laravel -g laravel -s /bin/sh -D laravel
RUN mkdir -p /var/www/html
RUN chown laravel:laravel /var/www/html
WORKDIR /var/www/html
RUN docker-php-ext-install pdo pdo_mysql

我这里也加了端口(compose.dockerfile):

php:
    build:
      context: .
      dockerfile: php.dockerfile
    container_name: php
    volumes:
      - ./src:/var/www/html:delegated
    ports:
      - "9000:9000"
     # Added next line:
      - "9001:9001"                     
    networks:
      - laravel

我试过将这个添加到 php.dockerfile 的末尾:

# Install essential build tools
RUN apk add --no-cache \
    git \
    yarn \
    autoconf \
    g++ \
    make \
    openssl-dev

# Install xdebug
RUN docker-php-source extract \
    && pecl install xdebug \
    && echo "xdebug.remote_enable=on\n" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
    && echo "xdebug.remote_autostart=on\n" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
    && echo "xdebug.remote_port=9001\n" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
    && echo "xdebug.remote_handler=dbgp\n" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
    && echo "xdebug.remote_connect_back=1\n" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
    && docker-php-ext-enable xdebug \
    && docker-php-source delete \
    && rm -rf /tmp/*

这是我在添加上面几行时得到的错误(看起来无关,但我想它打破了一些依赖):

mysql       | Version: '5.7.29'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  MySQL Community Server (GPL)
composer    | list [--xml] [--raw] [--format FORMAT] [--] [<namespace>]
composer    |
npm exited with code 1
composer exited with code

我尝试了在 google 上找到的其他东西。但是无法让它工作(因为我真的不明白我在做什么)。我认为上面的感觉就像是我得到的关闭,但也许我完全错了。

我 运行 它在 Windows 10,需要更多信息吗?

我找到了关于如何设置的说明 here。将其添加到 php.dockerfile:

的末尾
# Install base packages
RUN apk update
RUN apk upgrade

# xdebug with VSCODE
ENV XDEBUG_VERSION=2.9.2
RUN apk --no-cache add --virtual .build-deps \
        g++ \
        autoconf \
        make && \
    pecl install xdebug-${XDEBUG_VERSION} && \
    docker-php-ext-enable xdebug && \
    apk del .build-deps && \
    rm -r /tmp/pear/* && \
    echo -e "xdebug.remote_enable=1\n\
        xdebug.remote_autostart=1\n\
        xdebug.remote_connect_back=0\n\
        xdebug.remote_port=9001\n\
        xdebug.idekey=\"VSCODE\"\n\
        xdebug.remote_log=/var/www/html/xdebug.log\n\
        xdebug.remote_host=host.docker.internal" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini


# Change TimeZone
RUN apk add --update tzdata
ENV TZ=Europe/Bucharest

编辑: 您还应该在 docker-compose.yml 中删除 xdebug 端口(如果您添加了它)

For **Visual Studio Code** Here is the kaunch.json I used:
{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Listen for XDebug",
            "type": "php",
            "request": "launch",
            "port": 9001,
            "pathMappings": {
                "/var/www/html/public": "${workspaceFolder}/src/public"
            },
        }
    ]
}
RUN apk add --no-cache $PHPIZE_DEPS \
    && pecl install xdebug-2.9.2 \
    && docker-php-ext-enable xdebug  \