PhpStorm does not open websocket port when running Ratchet in XDebug in docker

Hello Everyone,

TL;DR

When I debug a PHP WebSocket server application through PhpStorm with XDebug in a docker container, I cannot connect to the socket. Running the app via CLI command everything works fine.

The issue

I have a docker container with a simple PHP app running a WebSocket server written with Ratchet.

When I run the app from the command line, everything works well:

php server.php

and the port I specified in the WebSocket configuration (2005) is correctly open:

bash-5.1# netstat -l
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:2005 0.0.0.0:* LISTEN

However

When I run the same app through PhpStorm XDebug configuration (both run and debug) no port 2005 is opened in the server. The application runs, but cannot be reached. When I run the debugger, I notice that PhpStorm creates a new temporary container for the debugger, and I feel that the app is run in that container (I can be wrong though, I am speculating) thus not running it in the container.

The result is that no matter where I try to connect to the socket (the WebSocket or a normal socket have the same issues) the connection is not established. I can reproduce this issue from the host machine as well as from inside the docker container.

The issue is that the app is not listening to the specified port in the expected container.

Questions:

  • Is it possible that the app is run in the temporary PhpStorm docker container? If this is the case, is there a way of opening a port in that specific container and redirect the call coming from the host there?
  • Am I completely wrong in my assumption and there is another issue I cannot see?

Setup:

  • PhpStorm 2021.2.2
  • PHP 8.0
  • xDebug 3.0.4

Please feel free to throw ideas on how I can debug a WebSocket app that, when run in debug mode, does not listen correctly.

Thanks

2 comments
Comment actions Permalink

Is it possible that the app is run in the temporary PhpStorm docker container?

The way PhpStorm runs your code depends on the type of the Docker interpreter you are using:

  • If it's a plain Docker interpreter - yes, it'll start a new container from the image you specified, and it'll lack some features of the container you've run manually.
  • If it's a Docker Compose interpreter in the run mode, the same - it'll start a new container, it'll be similar to the container created from the same docker-compose.yml service by you, but not the same one.
  • If it's a Docker Compose interpreter in the exec mode, PhpStorm will try to use the existing container.

How exactly do you start your container and the WebSocket server in it outside PhpStorm?

1
Comment actions Permalink

Hi Eugene Morozov

Thanks for your reply. You are 100% correct!

I had set up PhpStorm using the plain Docker Interpreter, which was starting a new container each time. Moving it to `Docker Compose` in the `exec` mode, the port is actually open in the correct container and the debugging works.

Thanks!

0

Please sign in to leave a comment.