Docker for #PHP with @Xdebug and @phpstorm is (or was) a bit tricky to setup. 


Here is a sample you can use:
* no Dockerfile needed
* composer for build jobs
* code execution with phpunit
A thread!



Here is a sample you can use:
* no Dockerfile needed
* composer for build jobs
* code execution with phpunit
A thread!


Instead of creating our own Dockerfile we use webdevops/php-dev image, which already comes with Xdebug (and other tools like blackfire) installed.
If you wonder about the path mapping:
docker-compose.yaml and .env live in a subfolder in this example.
If you wonder about the path mapping:
docker-compose.yaml and .env live in a subfolder in this example.
We map a local .env file for configuration. This will create these environment variables "inside" the docker container.
Now run the following commands to create/run the containers:
`docker-compose pull`
`docker-compose up -d`
no build required, as we use a pre-build image
Now run the following commands to create/run the containers:
`docker-compose pull`
`docker-compose up -d`
no build required, as we use a pre-build image
If you are on linux, check this little additional info: https://twitter.com/fn_schubert/status/1356310433674752003?s=20
As you might spotted, Xdebug is “off“ by default. So we need to enable it. Therefore create 2 entries in the “scripts“ section of composer.json.
with `composer test` Xdebug will only create coverage
with `composer test-debug` Xdebug will call Phpstorm to step debug
with `composer test` Xdebug will only create coverage
with `composer test-debug` Xdebug will call Phpstorm to step debug
But first we need to setup a Server in Phpstorm. Open Preferences and then go to
Languages & Frameworks > PHP > Servers
Add a new one with:
name: must be the same as specified in .env file
host: localhost or 0.0.0.0
port: doesn’t matter
path mapping: map absolute path /app
Languages & Frameworks > PHP > Servers
Add a new one with:
name: must be the same as specified in .env file
host: localhost or 0.0.0.0
port: doesn’t matter
path mapping: map absolute path /app
In the right top click on the little button that says on hover "Start listening for debug connections"
After that run:
`docker exec -it rest-php74 composer test-debug`
rest-php74 is the container_name specified in docker-compose
After that run:
`docker exec -it rest-php74 composer test-debug`
rest-php74 is the container_name specified in docker-compose
for more details on the used image, check:
https://dockerfile.readthedocs.io/en/latest/content/DockerImages/dockerfiles/php-dev.html
https://dockerfile.readthedocs.io/en/latest/content/DockerImages/dockerfiles/php-dev.html
If you want to check the full working example:
https://github.com/violines/rest-bundle/
follow the readme or head over to the folder "docker".
https://github.com/violines/rest-bundle/
follow the readme or head over to the folder "docker".