Listening port for Xdebug: setting up Xdebug for two Docker instances and two different projects

I am running two Docker instances and each of them has his own Xdebug and his own PHP of course they behave as an independent server each one. This is how I expose the port "9001" in my Dockerfile:

EXPOSE 80 9001

Having said that I am using "docker-compose" to build my stack and this is how my docker-compose.yml file looks like:

version: '3.4'
services:
erx:
image: reynierpm/arx_dev
ports:
- "80:80"
- "9002:9001"
volumes:
- ./applications/erx/:/var/www/html
restart: on-failure
environment:
VHOST_DOCUMENT_ROOT: /var/www/html
api:
image: reynierpm/arx_dev
ports:
- "81:80"
- "9003:9001"
volumes:
- ./applications/master-api/:/var/www/html
restart: on-failure
environment:
VHOST_DOCUMENT_ROOT: /var/www/html/public

From there I would expect that the port 9001 from erx would be mapped to port 9002 on the host and 9001 on api would be mapped to 9003 on the host, at least this is how Docker port mapping works (unless I am completely wrong). And this is how I have setup Xdebug on my IDE:

As soon as I run my stack and try to listen on port 9003, the IDE says there is already a connection listening on that port. I don't know what I am missing here. How do I setup Xdebug properly for both containers and both projects IDE's?

3 comments

Hi there,

Please, read the manuals (Docker and Xdebug) to see how xdebug works and what "exposing port" in Docker does...

 

In short: You do not need to expose xdebug port -- it's xdebug that connects to IDE and NOT other way around.

Right now, with xdebug port exposed this way .. Docker is the one who listens on your host OS (where you have PhpStorm running) .. so no wonder that PhpStorm is unable to listen on already occupied port.

0

HI there,

Let me see if I am following you. You want me to:

  • Remove the `EXPOSE 9001` from my Dockerfile and of course re-build the Docker image
  • Remove the mapping from my `docker-compose.yml` file

Is that correct?

Other thing that I have notice and I am not sure in how to fix if that even having the little bug icon enabled (meaning is on green) I can not see anything listening on port 9001 using `netstat` on Windows, any ideas?

0

>"9002:9001"

You don't need these kind of lines. You do not need to expose any xdebug port from docker side.

PhpStorm must be the one listening on xdebug port. So make it 9001 everywhere (in PhpStorm and php.ini in containers)

Xdebug must connect to PhpStorm on that port (so the IP address in php.ini must be the IP of the host as seen from within the container; "xdebug.connect_back = 1" may do the job for web based debug if you unsure on your host IP; for CLI -- it will be passed by IDE in command line; and if debugging CLI script where launch was initiated inside the container (e.g. cron job) -- then "xdebug.remote_host" only with "connect_back = 0")

Always check what xdebug log has to say -- where it tries to connect/does it tries to connect at all etc.

"netstat -abn" does the job for me (with phone handle icon in IDE)

0

Please sign in to leave a comment.