|
| 1 | +# Debugging |
| 2 | + |
| 3 | +The default Docker stack is shipped without a Xdebug stage. It's easy |
| 4 | +though to add [Xdebug](https://xdebug.org/) to your project, for development |
| 5 | +purposes such as debugging tests or api requests remotely. |
| 6 | + |
| 7 | +## Add a development stage to the Dockerfile |
| 8 | + |
| 9 | +To avoid deploying API Platform to production with an active Xdebug extension, |
| 10 | +it's recommended to add a custom stage to the `api/Dockerfile`. See the |
| 11 | +following diff for a working example. |
| 12 | + |
| 13 | +```diff |
| 14 | +diff --git a/api/Dockerfile b/api/Dockerfile |
| 15 | +index 745a8f9..432f0d9 100644 |
| 16 | +--- a/api/Dockerfile |
| 17 | ++++ b/api/Dockerfile |
| 18 | +@@ -1,11 +1,12 @@ |
| 19 | +# the different stages of this Dockerfile are meant to be built into separate images |
| 20 | +# https://docs.docker.com/compose/compose-file/#target |
| 21 | + |
| 22 | ++ARG BUILD_ENV=prod |
| 23 | +ARG PHP_VERSION=7.2 |
| 24 | +ARG NGINX_VERSION=1.15 |
| 25 | +ARG VARNISH_VERSION=6.0 |
| 26 | + |
| 27 | +-FROM php:${PHP_VERSION}-fpm-alpine AS api_platform_php |
| 28 | ++FROM php:${PHP_VERSION}-fpm-alpine AS api_platform_php_prod |
| 29 | + |
| 30 | +# persistent / runtime deps |
| 31 | +RUN apk add --no-cache \ |
| 32 | +@@ -51,6 +52,15 @@ RUN set -eux; \ |
| 33 | + \ |
| 34 | + apk del .build-deps |
| 35 | + |
| 36 | ++FROM api_platform_php_prod as api_platform_php_dev |
| 37 | ++ |
| 38 | ++ARG XDEBUG_VERSION=2.6.0 |
| 39 | ++RUN apk add --no-cache $PHPIZE_DEPS; \ |
| 40 | ++ pecl install xdebug-$XDEBUG_VERSION; \ |
| 41 | ++ docker-php-ext-enable xdebug |
| 42 | ++ |
| 43 | ++FROM api_platform_php_${BUILD_ENV} as api_platform_php |
| 44 | ++ |
| 45 | +COPY --from=composer:latest /usr/bin/composer /usr/bin/composer |
| 46 | +COPY docker/php/php.ini /usr/local/etc/php/php.ini |
| 47 | +``` |
| 48 | + |
| 49 | +Now when building the stack with the `BUILD_ENV=dev` flag, Xdebug is |
| 50 | +successfully available. |
| 51 | + |
| 52 | +Inspect the installation with the following command, the requested Xdebug |
| 53 | +version should be displayed in the output. |
| 54 | + |
| 55 | +```bash |
| 56 | +$ docker-compose exec php php --version |
| 57 | + |
| 58 | +PHP 7.2.8 (cli) (built: Jul 21 2018 08:09:37) ( NTS ) |
| 59 | +Copyright (c) 1997-2018 The PHP Group |
| 60 | +Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies |
| 61 | + with Zend OPcache v7.2.8, Copyright (c) 1999-2018, by Zend Technologies |
| 62 | + with Xdebug v2.6.0, Copyright (c) 2002-2018, by Derick Rethans |
| 63 | +``` |
| 64 | + |
| 65 | +## Configure Xdebug with docker-compose override |
| 66 | + |
| 67 | +Using a [override](https://docs.docker.com/compose/reference/overview/#specifying-multiple-compose-files) |
| 68 | + file named `docker-compose.override.yml` ensures that the production |
| 69 | +configuration remains untouched. |
| 70 | + |
| 71 | +As example, an override could look like this: |
| 72 | + |
| 73 | +```yml |
| 74 | +version: '3.4' |
| 75 | + services: |
| 76 | + php: |
| 77 | + build: |
| 78 | + args: |
| 79 | + BUILD_ENV: dev |
| 80 | + environment: |
| 81 | + # See https://docs.docker.com/docker-for-mac/networking/#i-want-to-connect-from-a-container-to-a-service-on-the-host |
| 82 | + # The `remote_host` below may optionally be replaced with `remote_connect_back` |
| 83 | + # |
| 84 | + # Choose some other port than `9000`, as this one is already occupied by FPM |
| 85 | + # Make sure to also change your listening port in the IDE to this value |
| 86 | + XDEBUG_CONFIG: remote_enable=1 |
| 87 | + remote_host=host.docker.internal |
| 88 | + remote_port=9999 |
| 89 | + idekey=PHPSTORM |
| 90 | + # This should correspond to the server declared in PHPStorm `Preferences | Languages & Frameworks | PHP | Servers` |
| 91 | + # Then PHPStorm will use the corresponding path mappings |
| 92 | + PHP_IDE_CONFIG: serverName=api-platform |
| 93 | +``` |
0 commit comments