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