From ce2ce13879ec1fcbeef4271ba54c6d24f1be3019 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joan=20L=C3=B3pez?= Date: Sat, 8 Jun 2024 17:53:03 +0200 Subject: [PATCH] feat: add xdebug to cli container --- .docker/wp-cli/Dockerfile | 29 +++++++++++++++++++++++++++++ README.md | 5 ++++- docker-compose.yml | 9 ++++++++- 3 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 .docker/wp-cli/Dockerfile diff --git a/.docker/wp-cli/Dockerfile b/.docker/wp-cli/Dockerfile new file mode 100644 index 0000000..946350a --- /dev/null +++ b/.docker/wp-cli/Dockerfile @@ -0,0 +1,29 @@ +ARG PHP_VERSION + +FROM wordpress:cli-php${PHP_VERSION:-8.1} + +# Switch to root user to ensure we have the necessary permissions +USER root + +RUN apk add --update linux-headers + +# Install Xdebug +RUN apk --no-cache add pcre-dev ${PHPIZE_DEPS} \ + && pecl install xdebug \ + && docker-php-ext-enable xdebug \ + && apk del pcre-dev ${PHPIZE_DEPS} + +# Configure Xdebug +RUN { \ + echo 'xdebug.mode=debug'; \ + echo 'xdebug.start_with_request=yes'; \ + echo 'xdebug.client_host=host.docker.internal'; \ + echo 'xdebug.client_port=9003'; \ + echo 'xdebug.log=/tmp/xdebug.log'; \ +} > /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini + +# Ensure the configuration is loaded +RUN echo 'zend_extension=xdebug.so' >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini + +# Switch back to the default user (if necessary) +USER www-data diff --git a/README.md b/README.md index a46c3ed..c79b4e9 100644 --- a/README.md +++ b/README.md @@ -109,7 +109,10 @@ the Usage section). ## How to Use Xdebug -Xdebug is enabled by default. To use it with Visual Studio Code, you have to +Xdebug comes pre-installed and activated in the wp and cli containers, allowing +debugging of website requests and WP-CLI commands. + +To use Xdebug it with Visual Studio Code, you have to include `?XDEBUG_SESSION=TRUE` in the URL you are browsing, and you have to use this `launch.json` settings: diff --git a/docker-compose.yml b/docker-compose.yml index 1b93df3..b594514 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -59,7 +59,14 @@ services: cli: container_name: ${NAME}-cli - image: wordpress:cli-php${PHP_VERSION:-8.2} + build: + context: . + dockerfile: .docker/wp-cli/Dockerfile + args: + PHP_VERSION: ${PHP_VERSION:-8.1} + environment: + XDEBUG_MODE: develop,debug + XDEBUG_CONFIG: "client_host=host.docker.internal" volumes_from: - wp links: