Configure xDebug on remote docker container

Docker is widely used for WEB development. However on Mac running such a heavy application as Magento 2 in Docker is not very smooth. Because Docker runs on Linux much better I tried setup everything on remote Ubuntu machine and have only IDE on Mac. In this article I would like to share my experience with configuring xDebug on such setup.

Speaking about Magento 2 and Docker I highly recommend Warden tool. With Warden it is super easy to install and run any version of Magento 2.

In my current setup with help of Warden I run Magento 2 in docker container on a remote Ubuntu machine. On Mac I need only IDE (PHPStorm) and files. Files can be synced using SFTP (PHPStorm can upload files automatically) or Mutagen.

While most of things in this to setup are easy to configure it took me some time to make xDebug working.

I thought that opening a simple SSH tunnel would work:

ssh -R 9003:localhost:9003 user@server.com
#xDebug 3 is using 9003 port

But unfortunately it doesn’t work. XDebug could not reach PHPStorm client on Mac.

Eventually I come to the solution that worked.

1. On Docker host machine (Ubuntu) forward port 9003 to another port (e. g. 9000).

ssh -g -L 9003:localhost:9000 127.0.0.1

2. Then on Mac open SSH tunnel:

ssh -R 9000:localhost:9000 user@ubuntu

That is it.

If PHPStorm is configured properly (see instructions for Warden) xDebug now should work.