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

问题描述

我的目标是将Laravel的git repo与php-fpm的xdebug一起使用: https://github.com/aschmelyun/docker-compose-laravel

使用此存储库时,我运行:

  1. docker-compose up -d --build网站
  2. docker-compose up

以下是仓库上方的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上运行它,需要更多信息吗?

解决方法

RUN apk add --no-cache $PHPIZE_DEPS \
    && pecl install xdebug-2.9.2 \
    && docker-php-ext-enable xdebug  \
,

我发现此说明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"
            },}
    ]
}

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...