J'ai créé mon propre Docker-Compose et Dockerfiles pour une application Laravel. Tout fonctionne comme prévu, mais je ne peux pas obtenir à Xdebug de travailler. J'ai traversé quelques tutoriels sur Xdebug avec Windows, mais pas de chance. Je retourne récemment à Windows après avoir exécuté Mac pendant de nombreuses années. Ma configuration:

  • Windows 10 Pro
  • VSCode
  • Élément de liste
  • WSL2
  • Docker Desktop

Docker-compose.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
        }
    ]
}

1
kurtfoster 30 mars 2021 à 09:49

2 réponses

Meilleure réponse

pecl installera xdebug 3, et tous vos paramètres sont toujours pour xdebug 2. Veuillez passer par le Guide de mise à niveau et ajustez vos paramètres.

3
Derick 30 mars 2021 à 08:17

Merci derick,

J'ai ajouté à docker-compose.yml dans la section environment sur la ligne 21

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

Et .env

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

Puis modifié la ligne 84 dans App.DockerFile

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

Cela fonctionne comme prévu maintenant.

0
kurtfoster 30 mars 2021 à 09:28