|
| 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 end of the `api/Dockerfile`. |
| 11 | + |
| 12 | +```Dockerfile |
| 13 | +# api/Dockerfile |
| 14 | +FROM api_platform_php as api_platform_php_dev |
| 15 | + |
| 16 | +ARG XDEBUG_VERSION=2.6.0 |
| 17 | +RUN set -eux; \ |
| 18 | + apk add --no-cache --virtual .build-deps $PHPIZE_DEPS; \ |
| 19 | + pecl install xdebug-$XDEBUG_VERSION; \ |
| 20 | + docker-php-ext-enable xdebug; \ |
| 21 | + apk del .build-deps |
| 22 | +``` |
| 23 | + |
| 24 | +## Configure Xdebug with Docker Compose Override |
| 25 | + |
| 26 | +Using an [override](https://docs.docker.com/compose/reference/overview/#specifying-multiple-compose-files) |
| 27 | + file named `docker-compose.override.yml` ensures that the production |
| 28 | +configuration remains untouched. |
| 29 | + |
| 30 | +As example, an override could look like this: |
| 31 | + |
| 32 | +```yml |
| 33 | +version: "3.4" |
| 34 | + |
| 35 | +services: |
| 36 | + php: |
| 37 | + build: |
| 38 | + target: api_platform_php_dev |
| 39 | + environment: |
| 40 | + # See https://docs.docker.com/docker-for-mac/networking/#i-want-to-connect-from-a-container-to-a-service-on-the-host |
| 41 | + # See https://github.com/docker/for-linux/issues/264 |
| 42 | + # The `remote_host` below may optionally be replaced with `remote_connect_back` |
| 43 | + XDEBUG_CONFIG: >- |
| 44 | + remote_enable=1 |
| 45 | + remote_host=host.docker.internal |
| 46 | + remote_port=9000 |
| 47 | + idekey=PHPSTORM |
| 48 | + # This should correspond to the server declared in PHPStorm `Preferences | Languages & Frameworks | PHP | Servers` |
| 49 | + # Then PHPStorm will use the corresponding path mappings |
| 50 | + PHP_IDE_CONFIG: serverName=api-platform |
| 51 | +``` |
| 52 | +
|
| 53 | +## Troubleshooting |
| 54 | +
|
| 55 | +Inspect the installation with the following command. The requested Xdebug |
| 56 | +version should be displayed in the output. |
| 57 | +
|
| 58 | +```bash |
| 59 | +$ docker-compose exec php php --version |
| 60 | + |
| 61 | +PHP 7.2.8 (cli) (built: Jul 21 2018 08:09:37) ( NTS ) |
| 62 | +Copyright (c) 1997-2018 The PHP Group |
| 63 | +Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies |
| 64 | + with Zend OPcache v7.2.8, Copyright (c) 1999-2018, by Zend Technologies |
| 65 | + with Xdebug v2.6.0, Copyright (c) 2002-2018, by Derick Rethans |
| 66 | +``` |
0 commit comments