Running PHPUnit within a Docker container with PhpStorm Running PHPUnit within a Docker container with PhpStorm docker docker

Running PHPUnit within a Docker container with PhpStorm


With PhpStorm now having better integration with Docker engine (including Docker for Mac), you can now just do the following (or read this article):

Command line:

  1. Pull the phpunit Docker image: docker pull phpunit/phpunit
  2. (Mac and maybe Windows) Bridge the Docker socket to the API_URL: socat -d TCP-LISTEN:2376,range=127.0.0.1/32,reuseaddr,fork UNIX:/var/run/docker.sock

Then Phpstorm:

  1. Configure connection to Docker engine:
    • Go to Settings -> Build, Execution, Deployment -> Docker
    • Create a new Docker configuration with API URL set to:
      • Linux: unix:///var/run/docker.sock
      • Windows and Mac: http://127.0.0.1:2376 or tcp://localhost:2376
  2. Configure the remote interpreter:
    • Go to Settings -> Languages & Frameworks -> PHP
    • Create a new PHP CLI interpreter by:
      • Clicking on ... then + and 'Remote...'
      • Select Docker with:
        • ServerImage:
        • Image name: phpunit/phpunit:latest
        • PHP executable: php
  3. Configure Phpunit:
    • Go to Settings -> Languages & Frameworks -> PHP -> PHPUnit
    • Create new Phpunit configuration (+ then 'By Remote interpreter...' and select
    • Set Use Composer Autoloader
    • Path to script: /opt/project/vendor/autoload.php
    • Default configuration file: /opt/project/phpunit.xml.dist
  4. Try to run your tests!


This works:

DIR=$(dirname $(readlink -f "$0"))docker run --rm --sig-proxy=true -v ${DIR}:${DIR} -w ${DIR} --pid=host php:cli php "$@"

just put it in some file, chmod +x it => just tested with idea and is recognized just fine :)

The trick to get all of phpstorms helper scripts to work really is to mount the directory the php executable resides in.In case this script is not in the folder of your phpunit executable and source code you will have to extend it to also mount those.

(important side note here: they must be mounted to the same folder in container and host obv :) )

Edit after a few months now, but maybe helpful to some:Worked this out with a WordPress example here:http://original-brownbear.github.io/2015/12/23/phpunit-docker-phpstorm.html


I know you say you heard ssh in docker containers is bad, but I think since PhpStorm doesn't support docker's exec to remotely run scripts it's not a bad option. You can set up a docker container as a remote php interpreter using SSH inside PhpStorm, then just set up a remote PHPUnit configuration.

You can see step-by-step guide here:
https://mickadoo.github.io/php,phpunit,docker/2016/10/12/phpunit-docker.html

Update: you don't need to use SSH anymore. PhpStorm now supports docker, you can run PHP unit tests just by using a PHP docker image with PHPUnit installed as a remote interpreter.