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

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

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

问题

我有以下的Dockerfile

  1. FROM php:7.2-apache
  2. RUN apt-get update && apt-get install -y \
  3. zlib1g-dev \
  4. git \
  5. unzip \
  6. && rm -rf /var/lib/apt/lists/ \
  7. && docker-php-ext-install zip pdo_mysql bcmath \
  8. && a2enmod rewrite \
  9. && pecl install xdebug-2.9.0 redis \
  10. && docker-php-ext-enable xdebug redis \
  11. && mv "$PHP_INI_DIR/php.ini-development" "$PHP_INI_DIR/php.ini" \
  12. && mv /var/www/html /var/www \
  13. && chown -R www-data:www-data /var/www/ \
  14. && ln -s /usr/local/bin/php /usr/bin/php
  15. COPY --from=composer:1.9 /usr/bin/composer /usr/bin/composer
  16. WORKDIR /var/www/drm-case
  17. CMD ["docker/apache/bootstrap.sh"]

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

  1. version: "2.4"
  2. services:
  3. case-v2-apache:
  4. container_name: local-dev
  5. image: local-dev:1.6 # 当更改Dockerfile时,增加此数字
  6. depends_on:
  7. mysql-server:
  8. condition: service_healthy
  9. volumes:
  10. - ${LOCAL_PATH}:/var/www:delegated
  11. - ${LOCAL_PATH}/docker/apache/conf.d:/usr/local/etc/php/conf.d
  12. - ${LOCAL_PATH}/docker/apache/conf-enabled/servername.conf:/etc/apache2/conf-enabled/servername.conf
  13. - ${LOCAL_PATH}/docker/apache/sites-available/000-default.conf:/etc/apache2/sites-available/000-default.conf
  14. ports:
  15. - "8009:80"
  16. logging:
  17. driver: "json-file"
  18. options:
  19. max-size: "10m"
  20. max-file: "3"
  21. build:
  22. context: ${LOCAL_PATH:-./local-dev}
  23. dockerfile: docker/apache/Dockerfile
  24. networks:
  25. - main
  26. environment:
  27. - VIRTUAL_HOST=local-dev.localhost
  28. - COMPOSER_MEMORY_LIMIT=-1
  29. - COMPOSER_AUTH=${COMPOSER_AUTH}
  30. - COMPOSER_ALLOW_SUPERUSER=1
  31. - 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:

  1. FROM php:7.2-apache
  2. RUN apt-get update && apt-get install -y \
  3. zlib1g-dev \
  4. git \
  5. unzip \
  6. && rm -rf /var/lib/apt/lists/ \
  7. && docker-php-ext-install zip pdo_mysql bcmath \
  8. && a2enmod rewrite \
  9. && pecl install xdebug-2.9.0 redis \
  10. && docker-php-ext-enable xdebug redis \
  11. && mv "$PHP_INI_DIR/php.ini-development" "$PHP_INI_DIR/php.ini" \
  12. && mv /var/www/html /var/www \
  13. && chown -R www-data:www-data /var/www/ \
  14. && ln -s /usr/local/bin/php /usr/bin/php
  15. COPY --from=composer:1.9 /usr/bin/composer /usr/bin/composer
  16. WORKDIR /var/www/drm-case
  17. 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):

  1. version: "2.4"
  2. services:
  3. case-v2-apache:
  4. container_name: local-dev
  5. image: local-dev:1.6 # increase this number when changing the Dockerfile
  6. depends_on:
  7. mysql-server:
  8. condition: service_healthy
  9. volumes:
  10. - ${LOCAL_PATH}:/var/www:delegated
  11. - ${LOCAL_PATH}/docker/apache/conf.d:/usr/local/etc/php/conf.d
  12. - ${LOCAL_PATH}/docker/apache/conf-enabled/servername.conf:/etc/apache2/conf-enabled/servername.conf
  13. - ${LOCAL_PATH}/docker/apache/sites-available/000-default.conf:/etc/apache2/sites-available/000-default.conf
  14. ports:
  15. - "8009:80"
  16. logging:
  17. driver: "json-file"
  18. options:
  19. max-size: "10m"
  20. max-file: "3"
  21. build:
  22. context: ${LOCAL_PATH:-./local-dev}
  23. dockerfile: docker/apache/Dockerfile
  24. networks:
  25. - main
  26. environment:
  27. - VIRTUAL_HOST=local-dev.localhost
  28. - COMPOSER_MEMORY_LIMIT=-1
  29. - COMPOSER_AUTH=${COMPOSER_AUTH}
  30. - COMPOSER_ALLOW_SUPERUSER=1
  31. - 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…

  1. FROM php:7.2-apache
  2. RUN apt-get update && apt-get install -y \
  3. zlib1g-dev \
  4. git \
  5. unzip \
  6. && rm -rf /var/lib/apt/lists/ \
  7. && docker-php-ext-install zip pdo_mysql bcmath \
  8. && a2enmod rewrite \
  9. && pecl install xdebug-2.9.0 redis \
  10. && docker-php-ext-enable xdebug redis \
  11. && mv "$PHP_INI_DIR/php.ini-development" "$PHP_INI_DIR/php.ini" \
  12. && mv /var/www/html /var/www \
  13. && chown -R www-data:www-data /var/www/ \
  14. && ln -s /usr/local/bin/php /usr/bin/php
  15. COPY --from=composer:1.9 /usr/bin/composer /usr/bin/composer
  16. RUN if [[ "$DISABLE_XDEBUG" == "1" ]] ; then mv "$PHP_INI_DIR/conf.d/xdebug.ini" "$PHP_INI_DIR/conf.d/xdebug.ini.disabled"
  17. WORKDIR /var/www/drm-case
  18. 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:

确定