How to setup PhpStorm with Xdebug on Docker [Tutorial Part 2]
In the second part of this tutorial series on developing PHP on Docker we're taking a good hard look at PhpStorm, Xdebug and how to run and debug scripts from within PhpStorm on Docker.
And just as a reminder, the first part is over at Setting up PHP, PHP-FPM and NGINX for local development on Docker.
Note: The setup that I am going to use is for demonstration purposes only! I do not recommend that you use it "as is" as your development setup. Some problems that I won't solve here include:
- everything is owned by root (no dedicated user; that will in particular be problematic for linux users)
- SSH login credentials are hard-coded in the container (inherently insecure)
host.docker.internalwill only exist for Windows and Mac users, NOT for unix users
There will be a another part of this series that will deal with all of those (and some more common) problems and aims at providing a consistent development environment for all developers in a team (regardless of the OS they are using). Please subscribe to the RSS feed to get automatic notifications when that part comes out :)
Table of contents
- Setup: The docker containers
- Run PHP via built-in Docker setup
- Run PHP on Docker via Deployment Configuration
Setup: The docker containers
We will only need the php-cli container for this part.
Luckily, we already have a good understanding on how to create the container, although we'll need to make some
adjustments to make everything work smoothly with PhpStorm. I'm gonna walk you through all the necessary changes,
but I'd still recommend to clone the corresponding git repository docker-php-tutorial
(unless you've already done that in part 1), checkout branch
build the containers now.
As in part 1, I'm assuming your codebase lives at
cd /c/codebase/ git clone https://github.com/paslandau/docker-php-tutorial.git cd docker-php-tutorial git checkout part_2 docker-compose docker-compose build
Further, make sure to open
/c/codebase/docker-php-tutorial as a project in PhpStorm.
In general, there are two ways to run PHP from PhpStorm using Docker:
- via the built-in Docker setup
- via Deployment Configuration (treating docker more or less like a VM)
Run PHP via built-in Docker setup
This is the "easier" way and should mostly work "out of the box". When you run a PHP script using this method, PhpStorm will start a docker container and configure it automatically (path mappings, network setup, ...). Next, the script in question is executed and the container is stopped afterwards.
Enable docker to communicate on port 2375
Open the Docker Setting in tab "General" and activate the checkbox that says "Expose daemon on tcp://localhost:2375 without TLS".
Configure Docker Server in PhpStorm
In PhpStorm, navigate to
Truncated by Planet PHP, read more at the original (another 21610 bytes)