Search code examples
dockerphpstormxdebug

Cannot connect to specific port from docker container to host machine on macOS


I'm trying to connect from the docker container to the host machine's port and it doesn't want to work for the specific port (in my case 9003).

On the host machine, I checked that the port is open:

lsof -nP -iTCP -sTCP:LISTEN

and I see the target port (9003):

COMMAND     PID               USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
phpstorm  32838 mypersonalusername   17u  IPv4 0x0000000000000000      0t0  TCP 127.0.0.1:63342 (LISTEN)
phpstorm  32838 mypersonalusername  266u  IPv4 0x0000000000000000      0t0  TCP *:9003 (LISTEN)

And when I run inside the docker container the next command:

nc -vz host.docker.internal 9003

I see that this port is not available but when I try to reach another port:

nc -vz host.docker.internal 63342

and it works:

host.docker.internal (192.168.65.254:63342) open

I tried to use the docker.for.mac.host.internal host, I checked the macOS Firewall settings and even disabled the firewall but it didn't help. I see the difference in the port definition (127.0.0.1:63342 vs *:9003) but I see no way to specify the host on the second one. Even with that, for some other applications, it doesn't matter and I can connect to ports with the * host (not for everything but still).

If it makes a difference I'm trying to establish an Xdebug connection from my container to PhpStorm.

I'm sure that only PHPStorm listens to the port, I tried to set a different one - the same result in the end. I don't expose the port from Docker. I know, the difference here. The issue is that Netcat returns no output and the exit code is 1 but it returns not immediately but it waits for something.

Could you please advise why it can happen and how to solve the issue?


Solution

  • In my particular case, the VPN protocol blocked the connection. I don't know how and why and how to debug it but in the end, I changed the protocol and everything started working as a charm.

    The only hint I got is to enable port listener from another application like:

    nc -l 900
    

    and see, that I couldn't connect here either.