Connect PHPStorm with Docker on Mac, Windows and Linux


Docker 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.

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.

Docker Host

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.

Docker Configuration

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.

Docker Host

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.

Docker port

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:

Docker Server

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:port, 192.168.99.100 is the IP address of the default Docker machine).

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:

Docker Server

Reference:

https://confluence.jetbrains.com/display/PhpStorm/Docker+Support+in+PhpStorm