启用/禁用Docker-Compose中的xDebug,是否可能?

huangapple go评论70阅读模式
英文:

Enable/Disable xDebug through Docker-Compose, is it possible?

问题

我有以下的Dockerfile

FROM php:7.2-apache
RUN apt-get update && apt-get install -y \
        zlib1g-dev \
        git \
        unzip \
    && rm -rf /var/lib/apt/lists/ \
    && docker-php-ext-install zip pdo_mysql bcmath \
    && a2enmod rewrite \
    && pecl install xdebug-2.9.0 redis \
    && docker-php-ext-enable xdebug redis \
    && mv "$PHP_INI_DIR/php.ini-development" "$PHP_INI_DIR/php.ini" \
    && mv /var/www/html /var/www \
    && chown -R www-data:www-data /var/www/ \
    && ln -s /usr/local/bin/php /usr/bin/php
COPY --from=composer:1.9 /usr/bin/composer /usr/bin/composer
WORKDIR /var/www/drm-case
CMD ["docker/apache/bootstrap.sh"]

这是使用前面的Dockerfile构建容器的docker-compose.yml文件(不是每次都用):

version: "2.4"

services:
  case-v2-apache:
    container_name: local-dev
    image: local-dev:1.6 # 当更改Dockerfile时,增加此数字
    depends_on:
      mysql-server:
        condition: service_healthy
    volumes:
      - ${LOCAL_PATH}:/var/www:delegated
      - ${LOCAL_PATH}/docker/apache/conf.d:/usr/local/etc/php/conf.d
      - ${LOCAL_PATH}/docker/apache/conf-enabled/servername.conf:/etc/apache2/conf-enabled/servername.conf
      - ${LOCAL_PATH}/docker/apache/sites-available/000-default.conf:/etc/apache2/sites-available/000-default.conf
    ports:
      - "8009:80"
    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "3"
    build:
      context: ${LOCAL_PATH:-./local-dev}
      dockerfile: docker/apache/Dockerfile
    networks:
      - main
    environment:
      - VIRTUAL_HOST=local-dev.localhost
      - COMPOSER_MEMORY_LIMIT=-1
      - COMPOSER_AUTH=${COMPOSER_AUTH}
      - COMPOSER_ALLOW_SUPERUSER=1
      - PHP_IDE_CONFIG=serverName=local-dev

在这一行中被复制的文件${LOCAL_PATH}/docker/apache/conf.d:/usr/local/etc/php/conf.d与xdebug相关,意味着它是启用和设置扩展的文件。

想知道是否有一种通过使用ARGENV变量来告诉Docker在启动容器时启用/禁用xDebug的方法?有人以前尝试过这样的事情吗?如果有的话,你能帮我提供一些想法吗?

英文:

I have the following Dockerfile:

FROM php:7.2-apache
RUN apt-get update && apt-get install -y \
        zlib1g-dev \
        git \
        unzip \
    && rm -rf /var/lib/apt/lists/ \
    && docker-php-ext-install zip pdo_mysql bcmath \
    && a2enmod rewrite \
    && pecl install xdebug-2.9.0 redis \
    && docker-php-ext-enable xdebug redis \
    && mv "$PHP_INI_DIR/php.ini-development" "$PHP_INI_DIR/php.ini" \
    && mv /var/www/html /var/www \
    && chown -R www-data:www-data /var/www/ \
    && ln -s /usr/local/bin/php /usr/bin/php
COPY --from=composer:1.9 /usr/bin/composer /usr/bin/composer
WORKDIR /var/www/drm-case
CMD ["docker/apache/bootstrap.sh"]

And this is the docker-compose.yml file that uses the previous Dockerfile to build the containers (not all the time though):

version: "2.4"

services:
  case-v2-apache:
    container_name: local-dev
    image: local-dev:1.6 # increase this number when changing the Dockerfile
    depends_on:
      mysql-server:
        condition: service_healthy
    volumes:
      - ${LOCAL_PATH}:/var/www:delegated
      - ${LOCAL_PATH}/docker/apache/conf.d:/usr/local/etc/php/conf.d
      - ${LOCAL_PATH}/docker/apache/conf-enabled/servername.conf:/etc/apache2/conf-enabled/servername.conf
      - ${LOCAL_PATH}/docker/apache/sites-available/000-default.conf:/etc/apache2/sites-available/000-default.conf
    ports:
      - "8009:80"
    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "3"
    build:
      context: ${LOCAL_PATH:-./local-dev}
      dockerfile: docker/apache/Dockerfile
    networks:
      - main
    environment:
      - VIRTUAL_HOST=local-dev.localhost
      - COMPOSER_MEMORY_LIMIT=-1
      - COMPOSER_AUTH=${COMPOSER_AUTH}
      - COMPOSER_ALLOW_SUPERUSER=1
      - PHP_IDE_CONFIG=serverName=local-dev

One of the files being copied in this line: ${LOCAL_PATH}/docker/apache/conf.d:/usr/local/etc/php/conf.d is xdebug related meaning is the one enabling and setting up the extension.

Wonder if there is a way to tell Docker by using ARG or ENV variables to enable/disable xDebug while starting the container? Has anyone tried such thing before? If so can you help me with some ideas?

答案1

得分: 3

你可以根据构建参数简单地移动/重命名xdebug配置文件...或者类似这样的操作?

英文:

You could simply move/rename the xdebug conf file based on a build arg…

FROM php:7.2-apache
RUN apt-get update && apt-get install -y \
        zlib1g-dev \
        git \
        unzip \
    && rm -rf /var/lib/apt/lists/ \
    && docker-php-ext-install zip pdo_mysql bcmath \
    && a2enmod rewrite \
    && pecl install xdebug-2.9.0 redis \
    && docker-php-ext-enable xdebug redis \
    && mv "$PHP_INI_DIR/php.ini-development" "$PHP_INI_DIR/php.ini" \
    && mv /var/www/html /var/www \
    && chown -R www-data:www-data /var/www/ \
    && ln -s /usr/local/bin/php /usr/bin/php
COPY --from=composer:1.9 /usr/bin/composer /usr/bin/composer

RUN if [[ "$DISABLE_XDEBUG" == "1" ]] ; then mv "$PHP_INI_DIR/conf.d/xdebug.ini" "$PHP_INI_DIR/conf.d/xdebug.ini.disabled"

WORKDIR /var/www/drm-case
CMD ["docker/apache/bootstrap.sh"]

… or something like that?

huangapple
  • 本文由 发表于 2020年1月6日 23:40:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/59614934.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定