Xdebug 3 Phpstorm Docker



How do you map an incoming xdebug connection when your directory structure is split between a public folder which is the entry point for CodeIgniter and the rest of the CodeIgniter framework code in the server folder? It looks like this: I was able to successfully create and run a docker deployment in PhpStorm: Deploying 'Compose: docker'. PhpStorm 2020.3 is now available! This major release includes full support for PHP 8, static analyzers PHPStan and Psalm, Xdebug 3, Tailwind CSS, collaborative development via Code With Me, and much more. Download PhpStorm 2020.3 Read on for details on all the new features and significant updates, along with a ton of GIFs! Small example project to show how to configure xdebug (2 or 3) with WSL2 and Docker. This solution works only if you use PhpStorm on WSL2 with X server! WSL2 change Linux IP every reboot time, so you should, add global variable (WSLIP) in your Linux system. Add content from.bashrc to your bashrc (/.bashrc) or.zshrc.

  1. Xdebug 3 Phpstorm Dockery
  2. Php Docker Xdebug Phpstorm
  3. Xdebug 3 Phpstorm Docker Free
  4. Phpstorm Docker Xdebug Not Working
  5. Xdebug 3 Phpstorm Docker Download
  6. Phpstorm Setup Xdebug

FROM php: 7.3 WORKDIR /var/www # Install dependencies RUN apt-get update # Install extensions RUN docker-php-ext-install mbstring bcmath # XDEBUG RUN pecl install xdebug RUN docker-php-ext-enable xdebug # This needs in order to run xdebug from PhpStorm ENV PHPIDECONFIG 'serverName=DockerApp' ADD php.ini /usr/local/etc/php.

July 26, 2018

To follow this tutorial, you must have the Xdebug extension installed on your container. In my example, I will use a Docker image of TheCodingMachine created by David Négrier. If you are starting a project, I recommend you to download one of our images here TheCodingMachine Docker PHP images

My base docker-compose.yml file looks like this:

Note: the PHP_EXTENSION_XDEBUG environment variable is specific to the thecodingmachine/php. It automatically configures Xdebug for the container.

Ports configuration

Keep in mind that PHPStorm opens port 9000 (by default) so Xdebug can connect to it. Our PHP container has Xdebug installed and Xdebug will try to connect to PHPStorm on port 9000 by default.

Out of the box, everything should be fine...

... except if port 9000 is already taken by another program on your host (your machine running PHPStorm).

Switching port

Port 9000 can be already used. In particular, if you are using PHP-FPM, the default port for PHP-FPM is 9000.This directly conflicts with the XDebug port.

If you want to use another port for Xdebug, follow the instructions below.

I recommend to use port 10000 instead.

Otherwise, just keep the default configuration port.

Registering your container server in PHPStorm

First, click on edit configurations, on the top right of PHPStorm window.

Then, follow the explanation in picture. All numbers are explained below.

(1) Click on add button

(2) Confirm it by clicking on PHP Web Page

(3) Define a name for your project

(4) Click on ... to add a debug server

(5) Once it's done, you are on debug server page. Click on '+' button to create a new one.

(6) Define a server name like docker_[your_project_name]

(7) Add your host on port 80 using Xdebug

(8) Don't forget to click on Use path mappings

(9) We need to add path mapping. Indeed, from the point of view of Xdebug, the PHP application runs in the '/var/www/html' (we are in a container!).But for PHPStorm, my project is in my host machine directory! So we need to map the root of your PHP application to the '/var/www/html' directory, as shown on the image above.

At this point, you should be able to put a breakpoint in your code, start a debugging session and get the application to stop on the breakpoint. Jump to chapter 'Run Xdebug' to see how to use Xdebug.

Xdebug CLI

All applications are not web-based. You might also want to debug a command-line application, from within your container.Let's see how to enable Xdebug for scripts (CLI)

In the docker-compose.yml, just add those environment variables

Remember that we are using our technical director's (David Négrier) images. That's why we can set PHP extensions, PHP .ini configurations into the docker-compose.yml.For more details, please look at this TheCodingMachine Docker PHP images.

(1) Enable the Xdebug extension

(2) If you are using a different Xdebug port, set your custom Xdebug port

(3) Add your Xdebug server name

(4) If you are using the default Xdebug port, please remove remote_port=[your_xdebug_port]. Otherwise, add your custom Xdebug port

Run Xdebug

Web

Place a breakpoint in your code and launch a debug session

A new windows should open and you should access to PHPStorm debug bar.

Scripts (CLI)

Click on Start Listening for PHP Debug connections

The button should appear like this :

Place a breakpoint in your code.

Now, just go into your php-apache container using docker exec -ti [container_id] bash and run your script manually.

Addentum: Installing Docker integration in PHPStorm

There is a very useful PHPStorm extension to quickly and efficiently manage your docker containers.

For example:

  • see the logs by container
  • have access to bash in a clique
  • quickly see environment variables
  • clear or restart containers

Before starting

  • You should check that the 'Docker integration' is enabled on your IDE
  • Start the Docker daemon on you machine
  • Move to your favorite Docker project in PHPStorm

Configuring Docker

Now everything should be good, just go to Preferences... > Languages and Frameworks > PHP

(1) Click on ... for openning the remote PHP interpreter

(2) Add a new CLI interpreter by clicking on From Docker, Vagrant, VM, Remote...

(3) Click on Docker (remember that you should start Docker daemon)

(4) Add a new server

(5) Select a server name

(6) Choose how to connect on Docker

(7) Make sure the connection is successful

(8) Select your favorite default image

(9) You need to set the path mapping for your code. By default, it's /opt/project in the container, just change it to /var/www/html

Done! Just click on the Docker button on the bottom of PHPStorm window.

About the author

Alexis is an experienced technical project manager who manages several projects including Eco-Emballages (CITEO). Graduate of EPITECH, Alexis is specialized in PHP frameworks (Symfony, Laravel, Mouf) and he masters Docker.

Related articles

Setting up a XDebug debugging environment for PHP / WAMP / Eclipse PDTJuly 8, 2010

Latest articles

Releasing GraphQLite 4: GraphQL in PHP made easyJanuary 6, 2020
Xdebug
Getting started with TDBM 5.1 and SymfonyAugust 6, 2019

Open-source matters

At TheCodingMachine, we are using open-source tools daily to build our projects. And since we and our clients are ripping huge benefits from these open-source products, we also try to 'give back' to the open-source community.

Freelance Developer?

At TheCodingMachine, we are always looking for top freelance developers to work with us on our next state-of-the-art project.
Interested in developing quality projects? We love good work and we pay fairly.

We are hiring!

Living in France? We are actively looking for developers and project leaders in our Paris and Lyon office.

About us

At TheCodingMachine, we strive to develop websites , extranets, intranets and business web applications in PHP and JavaScript for a wide range of clients.
Do not hesitate to contact us!

Best practices

When writing code, we expect it to be of the best possible quality. To enforce this quality, we redacted our own 'best practices' described in a dedicated website.
We are happy to share those with you!

Packanalyst

We are the happy parents of Packanalyst, a free online service that lets you search any possible PHP class or interface referenced on Packagist.
Pretty useful for open-source developer to keep an eye on who is using what class/interface of your packages.

Skip to end of metadataGo to start of metadata

Redirection Notice

Icon
This page will redirect to https://www.jetbrains.com/help/phpstorm/docker.html in about 2 seconds.
Icon

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.

This tutorial describes Docker support in PhpStorm, which includes debugging PHP web applications (running in the Docker container), inspecting, managing containers and viewing running processes.You can also search through logs, start and stop containers, and perform basic container management.

  • Prerequisites
  • PhpStorm & Docker Integration Configuration
  • Working with Docker in PhpStorm

Docker installation

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:

  • Installing Docker engine on Windows;
  • Installing Docker engine on Mac OS X;
  • Installing Docker engine on Linux (Ubuntu, other distributions-related instructions are available as well);
  • You can also install Docker on various cloud platforms (e.g. read tutorials on installing Docker on Amazon EC2, Google Cloud Platform, and Microsoft Azure).

Docker plugin is bundled in PhpStorm since 2016.3.

Prerequisites for Mac for PhpStorm 2016.3 and below.

  1. Use the standard MacOSX terminal to execute asocat TCP-LISTEN:2375,reuseaddr,fork,bind=localhost UNIX-CONNECT:/var/run/docker.sockcommand to allow older versions of PhpStorm to connect to Docker;

    1. install socat as described at http://macappstore.org/socat/
    2. run socat TCP-LISTEN:2375,reuseaddr,fork,bind=localhost UNIX-CONNECT:/var/run/docker.sock
    3. enter tcp://localhost:2375 as an API URL in IDE
    4. (note: you may use any other port instead of 2375)


    Note: Since PhpStorm 2017.1 you can connect via Unix socket directly.

Configuring PhpStorm to work with Docker

  1. Open Settings / Preferences | Build, Execution, Deployment | Docker and create Docker configuration with a + button.
  2. You should provide the configuration name (Docker in our case), API URL (tcp://localhost:2375for Windows and for Mac OS X if using PhpStorm older than 2017.1, unix:///var/run/docker.sock for Mac OS X and Linux).

  3. Apply the configuration and close the Settings / Preferences dialog.
  4. Create a new Docker DeploymentRun/Debug Configuration invoking the dialog from the Run | Edit configurations... menu:
    Then, create a Docker Deployment configuration with a + button:
  5. Provide all the necessary parameters on the Deployment tab. In our case we've provided Run/Debug Configuration Name (Start Docker in our case), selected the server (Docker), selected the Deployment method to be Dockerfile, provided Image tag (mysite) and Container name (Docker_Xdebug):

  6. Provide all the necessary parameters and configurations on the Container tab. 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 (Container port: 80, Protocol: tcp, Host IP: empty, Host port: 8080):
    In addition, we can configure links, volume bindings, environment variables, and much more.

  7. Apply the Run/Debug Configuration and close the dialog.

Xdebug 3 Phpstorm Dockery

Icon

At this step it's important to note that that there are many ways to create/configure Docker containers and VM images, and we'd recommend to always refer to the Docker documentation on the official web site.

For the purposes of this tutorial we're using Dockerfile and Apache configuration file. We've also created an index.php file with phpinfo(); to be deployed to the Docker container. We're making some significant configuration in those Dockerfile and Apache configuration file, so you're recommended to have a look at them. You can download entire project used in this demo or separate config files (apache-config.conf, Dockerfile) 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:

Php Docker Xdebug Phpstorm

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 the process is completed, and our Docker_Xdebug container status turns green, we can check how it works in the browser. You should be able to open it by the URL http://localhost:8080/. If you can't see the index.php execution results in the browser (containing phpinfo(); in our case), please check that you have specified the correct port bindings on the previous steps.

Xdebug 3 Phpstorm Docker Free

In our example, everything is running fine on the port we've expected the web app to be:

Managing Docker containers and other Docker-related actions in PhpStorm

From the Application Servers tool window, it’s easy to inspect containers and view running processes. You can also search through logs, start and stop containers, and perform basic container management like creating and deleting containers. Each deployment in Docker is assigned a unique container ID - these are initially temporary containers, although they can be committed and saved for further distribution. On the Docker Hub registry, there are many such images available for you to try.

Images in Docker are read-only - once committed, any changes to a container’s state will become part of a new image. When you have a stable build on one instance of Docker (on your development machine, staging server, or a cloud), reproducing the exact same build is as simple as (1) committing the Docker container, (2) pushing it to a registry (public or private), then (3) pulling the same image to another instance of Docker, running - wherever.

Debugging the PHP web application running in the Docker container

Assuming that you already run the Docker container now (and everything worked well on the previous steps), you should now able to open your PHP web application in the browser by http://host:port URL (http://localhost:8080/ in our case). The major difficulty 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:

Phpstorm Docker Xdebug Not Working

In the example above we're modifying /etc/php/7.0/apache2/php.ini providing a path to Xdebug extension, and some other Xdebug parameters (remote_enable and remote_host). 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, 192.168.2.117 in our case).

Xdebug 3 Phpstorm Docker Download

Configuration for Zend Debugger is similar, please see a full tutorial on installing Xdebug and installing Zend Debugger (there's much more information on required parameters and options).

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

Phpstorm Setup Xdebug

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