Xdebug 无法处理 Docker Alpine Laravel 8 设置

Xdebug not working on Docker Alpine Laravel 8 setup

我已经为 Laravel 应用创建了自己的 docker-compose 和 Docker 文件。一切都按预期工作,但我无法让 Xdebug 工作。我已经 运行 通过一些关于 Xdebug 的教程 Windows,但运气不好。在 运行 宁 Mac 多年后,我最近回到 Windows。 我的设置:

docker-composer.yml

# For more information: https://laravel.com/docs/sail
version: '3'
services:
    #  The Application
    app:
        container_name: 'mybot_laravel_app'
        build:
            context: './'
            dockerfile: 'docker/app.dockerfile'
        networks:
            - 'frontend'
            - 'backend'
        working_dir: '/var/www'
        volumes:
        - './:/var/www'
        env_file: '.env'
        environment:
            DB_HOST: ${DB_HOST:-mybot_mysql_database}
            REDIS_HOST: ${REDIS_HOST:-cache}
            APCU_VERSION: 
            XDEBUG_CONFIG: ${XDEBUG_CONFIG:-""}
    # The Web Server
    web:
        container_name: 'mybot_nginx_server'
        build:
            context: './'
            dockerfile: 'docker/web.dockerfile'
        networks:
            - frontend
        volumes_from:
            - app
        ports:
        - '${APP_PORT:-80}:80'
    # The Database
    database:
        container_name: 'mybot_mysql_database'
        networks:
            - backend
        image: 'mysql:8.0'
        command: --default-authentication-plugin=mysql_native_password
        volumes:
        - 'dbdata:/var/lib/mysql'
        environment:
            MYSQL_DATABASE: ${DB_DATABASE:-mybot}
            MYSQL_USER: ${DB_USERNAME:-root}
            MYSQL_PASSWORD: ${DB_PASSWORD:-""}
            MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASSWORD:-""}
            MYSQL_ALLOW_EMPTY_PASSWORD: 'no'
        ports:
        - '${FORWARD_DB_PORT:-3306}:3306'
        healthcheck:
          test: ["CMD", "mysqladmin", "ping"]
    redis:
        image: 'redis:alpine'
        ports:
            - '${FORWARD_REDIS_PORT:-6379}:6379'
        volumes:
            - 'redis:/data'
        networks:
            - backend
        healthcheck:
          test: ["CMD", "redis-cli", "ping"]
    mailhog:
        image: 'mailhog/mailhog:latest'
        ports:
            - '${FORWARD_MAILHOG_PORT:-1025}:1025'
            - '${FORWARD_MAILHOG_DASHBOARD_PORT:-8025}:8025'
        networks:
            - backend
networks:
    frontend:
        driver: bridge
    backend:
        driver: bridge
volumes:
    dbdata:
    redis:

app.dockerfile

FROM php:7.4-fpm-alpine

COPY composer.lock composer.json /var/www/

COPY database /var/www/database

WORKDIR /var/www

RUN apk add --no-cache autoconf gcc g++ libtool make \
        && apk update \
        && apk add --no-cache --virtual .deps \
        mysql-client \
        git \
        zip \
        libzip-dev \
        icu-libs \
        zlib \
        zlib-dev \
        openssh \
        imagemagick \
        imagemagick-libs \
        imagemagick-dev \
        libpng-dev \
        libwebp-dev \
        libxpm-dev \
        libjpeg-turbo-dev \
        freetype-dev \
        && apk del --no-cache autoconf gcc g++ libtool make \
        && apk del --no-cache libmcrypt-dev

COPY . /var/www

RUN chown -R www-data:www-data \
        /var/www/storage \
        /var/www/bootstrap/cache

ARG APCU_VERSION
ENV APCU_VERSION=${APCU_VERSION:-5.1.20}

RUN set -xe \
        && apk add --no-cache --virtual .build-deps \
                $PHPIZE_DEPS \
                icu-dev \
        && docker-php-ext-install \
                intl \
                pdo_mysql \
        && pecl channel-update pecl.php.net \
        && pecl install \
                apcu-${APCU_VERSION} \
                apcu_bc \
                imagick \
                redis \
        && docker-php-ext-enable --ini-name 20-apcu.ini apcu \
        && docker-php-ext-enable --ini-name 21-apc.ini apc \
        && docker-php-ext-enable --ini-name 05-opcache.ini opcache \
        && docker-php-ext-enable --ini-name 20-imagick.ini imagick \
        && docker-php-ext-enable --ini-name 20-redis.ini redis

RUN set -xe \
        apk add --no-cache --virtual .build-deps zlib-dev \
        && docker-php-ext-install exif  \
        && docker-php-ext-install pcntl  \
        && docker-php-ext-install bcmath \
        && docker-php-ext-configure gd --with-freetype --with-jpeg --with-webp --with-xpm --with-webp \
        && docker-php-ext-install -j$(nproc) gd \
        && apk del --no-cache zlib-dev 

RUN apk add --no-cache --virtual .build-deps \
        libmcrypt \
        libltdl  \
        && apk add --no-cache --virtual .build-deps $PHPIZE_DEPS libmcrypt-dev \
        && yes '' | pecl install -f mcrypt \
        && docker-php-ext-enable mcrypt

RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" \
    && php -r "if (hash_file('SHA384', 'composer-setup.php') === '756890a4488ce9024fc62c56153228907f1545c228516cbf63f885e036d37e9a59d27d63f46af1d4d07ee0f76181c7d3') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;" \
    && php composer-setup.php \
    && php -r "unlink('composer-setup.php');" \
    && mv composer.phar /usr/local/bin/composer

RUN apk add --no-cache $PHPIZE_DEPS \
    && pecl install xdebug \
    && docker-php-ext-enable xdebug
RUN echo "zend_extension=$(find /usr/local/lib/php/extensions/ -name xdebug.so)" > /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
    && echo "xdebug.remote_enable=1" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
    && echo "xdebug.mode=debug" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
    && echo "xdebug.remote_autostart=1" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
    && echo "xdebug.remote_connect_back=0" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
    && echo "xdebug.remote_host=docker.for.mac.localhost" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
    && echo "xdebug.max_nesting_level=1500" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini

RUN apk add --update npm

CMD ["php-fpm"]

launch.json

{
    // 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": 9000,
            "pathMappings": {
                "/var/www/public": "${workspaceFolder}/public",
                "/var/www": "${workspaceFolder}",
            },
            "hostname": "localhost",
            "xdebugSettings": {
                "max_data": 65535,
                "show_hidden": 1,
                "max_children": 100,
                "max_depth": 5
            }
        },
        {
            "name": "Launch currently open script",
            "type": "php",
            "request": "launch",
            "program": "${file}",
            "cwd": "${fileDirname}",
            "port": 9000
        }
    ]
}

pecl 将安装 Xdebug 3,您的所有设置仍然适用于 Xdebug 2。请检查 Upgrade Guide 并调整您的设置。

感谢@derick,

我在第 21 行的 environment 部分添加了 docker-compose.yml

            XDEBUG_MODE: ${XDEBUG_MODE:-""}
            XDEBUG_SESSION: ${XDEBUG_SESSION:-""}

和.env

XDEBUG_CONFIG="client_host=host.docker.internal client_port=9000"
XDEBUG_MODE=debug
XDEBUG_SESSION=vscode

然后在 app.dockerfile

中修改了第 84 行
RUN echo "zend_extension=$(find /usr/local/lib/php/extensions/ -name xdebug.so)" > /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
    && echo "xdebug.start_with_request=yes" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini

现在一切正常。