Run/Debug PHP in Docker Container

I have Apache and PHP running in a docker container within a boot2docker VM, and I'm not sure how to tell PhpStorm to run php scripts or PHPUnit on that server.

E.g., for running codeception tests on a local server, I would create a debug configuration that uses a PHP Script:

File: vendor/bin/codecept
Arguments: run

I can get the same thing to run in docker using a "Bash" configuration instead of a "PHP Script" configuration.

Script: exec
Interpreter path: /usr/local/bin/docker
Program arguments: my_container bach -c "cd /path/to/tests/; vendor/bin/codecept run"

But by doing it this way, I lose the ability to debug the script.

Is there a way to tell PhpStorm to use my dockerized PHP?

4 comments
Comment actions Permalink

Hi there,

How does your Docker container communicates with your OS (especially the file access/transfer)?  (sorry, never used docker myself).

I've read that SSH is not needed/not recommended when working with docker containers as it is built on different principles ... but if you do have SSH installed there .. and transfering files over SFTP (for example) then using Remote PHP Interpreters will allow you to execute and debug your scripts from IDE using standard Run/Debug functionality.

0
Comment actions Permalink

Boot2docker mounts the /Users directory from the host OS (OS X 10.9) inside its VM, and then I have a subdirectory mounted in my docker container at /srv/www/. So there's no real file transfer going on.

I can get an SSH shell in the container by running "docker exec -it my_container bash". HTTP requests go through the boot2docker VM's IP address (domains are mapped thither using dnsmasq), which has port 80 (among others) forwarded from the container running PHP+Apache.

Any other details that will help? I haven't figured out how to configure it as a remote PHP interpreter.

0
Comment actions Permalink
I haven't figured out how to configure it as a remote PHP interpreter.


In order to use PhpStorm's Remote PHP Interpreters you have to have access over "normal" SSH.

If you can connect to your docker via "Tools | Start SSH session..." .. then you can use Remote PHP Interpreters, otherwise -- no.

-----

My only other suggestion (just ideas -- have not used docker myself) is to treat docker as normal remote server with no SSH support and you need to debug a CLI script.

Links:


For this:
1) Configure xdebug to attempt debugging every script: xdebug.remote_autostart = 1 (since we cannot say "debug this now" directly from IDE -- unless you can trigger this on/off via shell script somehow)

2) Configure xdebug with correct IP address (the IP of machine where IDE is running -- as seen from docker): xdebug.remote_host . Alternatively you may try xdebug.remote_connect_back = 1 -- auto detection may work just fine

3) Make sure that xdebug is able to establish outgoing connection to your IDE (yes, it's xdebug who connects to IDE) -- allow outgoing connections on xdebug port (TCP 9000 by default) if necessary.

4) Export server name (as described in the aforementioned ticket -- this is to make PhpStorm able to recognize what connection it is)

5) Activate "phone handle" icon in IDE -- PhpStorm now listens for incomming PHP debug connections

6) Create Server entry ("Settings (Preferences on Mac) | Languages & Frameworks | PHP | Servers") -- we need proper path mappings (as script is executed remotely)
  a) this step could be skipped (depends) -- IDE should offer help with this when it will receive incoming debug connection
  b) keep in mind -- xdebug always works with final/resolved paths .. so keep in mind any symbolic links on remote side

7) Launch your script and see if it works (debug). If not -- collect xdebug logs to start with: http://xdebug.org/docs/all_settings#remote_log

P.S.
To check if xdebug is able to connect to IDE from within docker -- just use telnet or similar tool:
1) Activate "phone handle" icon in IDE
2) Use telnet to connect to IP:port (where IP: IP of computer where IDE is running as seen from docker; port -- xdebug port)

If you are unable to connect then there must be some sort of firewall or your docker does not allow outgoing connections.

0
Comment actions Permalink

Yes, that did it. I already had port 9090 exposed for xdebug connections (and had it working when debugging requests from the browser). Without having to make any additional changes to the docker container, I added some parameters to the script.

Script: exec
Interpreter path: /usr/local/bin/docker
Program arguments: my_container bash -c "cd /path/to/tests/; XDEBUG_CONFIG='idekey=PHPSTORM' PHP_IDE_CONFIG='serverName=my.server' php -dxdebug.remote_autostart=1 -dxdebug.remote_host=192.168.1.64 vendor/bin/codecept run"

A bit of a mouthful, but gets the job done. Thanks, Andriy.

0

Please sign in to leave a comment.