Dockerized PHP Development with PHPStorm

Ridwan Shariffdeen
Docker Captain
Published in
5 min readJan 14, 2017

--

Debugging is the most annoying part of code development and nobody enjoys the process especially when it is not your code. In order to develop an application smoothly you need to setup the right tools correctly that help make the process a little less painful. This get even worse if you are using Docker to setup your development environment and the tools you usually need are not promptly available at the command line. In this article, I will show how to integrate docker components in PHPStorm to ease your pain.

Docker Environment

Docker is an eco-system for building, shipping and running applications in multiple environments, giving programmers, devOps and sysadmins the common box they need to configure in order to get their job done in their software development pipeline. You can easily setup your dev environment using Docker for multiple operating systems (Windows, RedHat, Ubuntu etc), multiple versions of applications (PHP 5.5, 7 etc) and multiple application services (Java, Ruby, Python etc). Having Docker to take care the environment problem we can leverage it’s benefit to focus on development.

But the problem is that most of the IDE’s are developed to run on top of the host system and very few support containerized environments. But not all hope is lost, most IDE’s are now providing integration support for Docker. I will be using PHPStorm 2016.3 to demonstrate the idea.

PHPStorm & Docker Integration

Once you have configured Docker to deploy your development environment, we can start configuring PhpStorm to work with Docker. First we need to configure the Docker daemon with our PhpStorm, this could either be in a remote host using API or directly in your local machine socket. Go to file-> settings -> Build, Execution, Deployment -> Docker

Click on the + icon to add a Docker daemon and setup the correct configuration. In your local machine docker can be accessed using the unix socket, for docker-compose you can give the absolute path for the executable or leave the command name if its configured with your profile. Once you have a Docker daemon configured, the next step is to configure which image we should use to run our application for debugging or testing purposes. In your settings menu go to Languages & Frameworks -> PHP. Click on configure CLI interpreter,

Select the Docker server you configured earlier and it will list the images you currently have pulled in your machine. You can select one image from existing list or you can type the image name you want the interpreter to be on. Once you select the image PHPStorm will automatically detect the PHP version and the Debugger version in the image.

Finally, we need to configure phpunit so within the IDE itself you can execute test and do various test activities that are supported by PHPStorm. In the settings windows go to Languages & Frameworks -> PHP -> PHPUnit and add a PHPUnit by clicking the + button and select Remote Interpreter, in the drop down you will find the interpreter we just configured, select the settings and provide the path to the phpunit program.

Your docker environment should provide support for Xdebug in order to configure the development workflow properly. Using xdebug we can make our lives easier as every time we run a script in PHP, information will be sent to our IDE and it will display it in an informative view. This is a sample configuration for xdebug.ini

xdebug.remote_enable=on
xdebug.remote_autostart=on
xdebug.remote_connect_back=off
xdebug.remote_handler=dbgp
xdebug.profiler_enable=0
xdebug.profiler_output_dir="/var/www/html"
xdebug.remote_host=172.17.0.1
xdebug.remote_port=9000

So assuming everything is configured inside the container, we will enable Debugging in PhpStorm as follow. Open the project via PHPStorm, we need to configure the container where our code is running. Add all containers (if multiple used) so it would be easy when we need to configure the debugging options. Go to File->settings and search for the keyword ‘servers’, you will get a screen like this;

PHPStorm Settings : Servers

Click on the + sign and add the containers by setting a name you prefer, and provide the IP address of the container (which you can obtain using Docker command docker inspect piped to grep using a regex for easiness) and the port its running (Apache port, Nginx port etc). You should tick the option “Use path mappings” and provide the host path and absolute path inside the container. For example your code may reside in your host machine in “/home/user/workspace/php-app” and inside the container it will be in your web server Document root, something like “/var/www/html/php-app”.

Configuring containers with different PHP versions

Everything is set for you to run your tests and debug your code using any PHP version thanks to Docker. There are many more features PHPStorm offers you to get the power of Docker, you can even run Docker commands view Docker containers, you can even debug containers itself inside using the IDE. All in All PHPStorm is a great IDE which provides just everything you need when your developing your application.

--

--