Docker Support in PhpStorm

large_v-transDocker is an open platform for building, shipping and running distributed applications. It gives programmers, development teams and operations engineers the common toolbox they need to take advantage of the distributed and networked nature of modern applications.

With the new Docker integration plugin for PhpStorm, you can add Docker support to existing projects, view logs, manage Docker containers, and debug PHP web applications from right inside PhpStorm.

In this blog post we overview some of the most important features and configurations; for more details please refer to the full tutorial on Docker Support in PhpStorm.

Prerequisites

First of all, you’ll need to install Docker and related tools so that you can take advantage of the Docker integration in PhpStorm. Please refer to the Docker documentation to get more information about the installation process for various platforms or the cloud.

You will also need to install the Docker integration plugin in PhpStorm (in Settings /Preferences | Plugins, click the Install JetBrains plugin…).

PhpStorm & Docker Integration Configuration

To start using Docker, the default Docker machine should be launched using the Docker Quickstart Terminal (Windows / Mac OS X, for Linux you need to use a standard terminal executing a sudo docker run hello-world command). At the next steps we’ll need some of the parameters of this machine environment, so we’ll need to run docker-machine env default console command to get all the necessary parameters (Windows / Mac OS X, for Linux you’ll need to use a standard IP address 172.17.42.1), such as DOCKER_HOST and DOCKER_CERT_PATH:

As soon as we have Docker running (and all the parameters fetched for future use), we can start configuring PhpStorm to work with Docker.

Create a Docker configuration with the + button in Settings / Preferences | Build, Execution, Deployment | Clouds:

We’ll need to provide the configuration name (Docker), API URL (https://192.168.99.100:2376, taken from the previous step), and Certificates folder (/Users/mikhailvink/.docker/machine/machines/default).

Please note that you will need to provide a socket path in the API URL field on Linux machines (unix:///var/run/docker.sock by default).

Next we need to create a new Docker Deployment Run/Debug Configuration in the Run | Edit configurations… menu.

Provide all the necessary parameters on the Deployment and Container tabs:

At this point we are interested in exposing port 80 of the container to be available from our local machine, so we should configure a port binding for that:

There are many ways to create/configure Docker containers and VM images; please refer to the Docker documentation on their official website.

For the purposes of this tutorial we’re making some configuration in the Dockerfile and Apache configuration file, so do have a look at them. You can download the entire project used in this demo or separate config files (apache-config.confDockerfile) which then need to be placed in the project root folder.

Running the Docker from PhpStorm

As all the tools are installed and the integration is configured, the recently created Start Docker Run/Debug Configuration can be launched. The Application Servers tool window will be opened updating you on the provisioning status and current state of all your Docker containers:

As soon as our Docker_Xdebug container’s status turns green, we can check how it works in the browser. You should be able to open it with a URL similar to http://192.168.99.100:8080/ (http://host:port192.168.99.100 is the IP address of the default Docker machine).

Managing Docker containers and other Docker-related actions in PhpStorm

From the Application Servers tool window, it’s easy to inspect containers and view the running processes. You can also search through logs, start and stop containers, and perform basic container management like creating and deleting containers.

Debugging PHP web application running in the Docker container

The main challenge in getting Xdebug (or Zend Debugger) working with PhpStorm and Docker integration is the correct configuration of the Docker container.

In our case we’re using a Dockerfile (we’ve already shown this config earlier and provided links to download it) to configure the container, including Xdebug-specific parameters, such as:

#Set up debugger
RUN echo "zend_extension=/usr/lib/php5/20131226/xdebug.so" >> /etc/php5/apache2/php.ini
RUN echo "xdebug.remote_enable=1" >> /etc/php5/apache2/php.ini
#Please provide your host (local machine IP) instead of 192.168.2.117
RUN echo "xdebug.remote_host=192.168.2.117" >> /etc/php5/apache2/php.ini

Please note that xdebug.remote_host value should be replaced with your local machine IP address which is visible from the Docker container (where PhpStorm is running, in our case 192.168.2.117).

Don’t forget to re-run Start Docker Run/Debug Configuration so that all the changes are applied.

As soon as all the configs are in place, the debugging process can be triggered by following this tutorial starting with step 2 (start Listening for PHP Debug Connections, set a breakpoint in the source code, and start a debugging session in the browser, reload the current page, debug) to get the debugger up and running in a few moments:

Please have a look at the full tutorial on Docker Support in PhpStorm for more information.

Please share your feedback on Docker support in PhpStorm in the forum, comments to this blog post, or our issue tracker. Thanks!

Develop with pleasure!
JetBrains PhpStorm Team

This entry was posted in Cool Feature, Early Access Program, Tutorial and tagged , , , , , , . Bookmark the permalink.