'Closed ports on docker host (OSX) are staying/reporting open on the internal docker network

After upgrading Docker to 4.6.0 on OSX 12.3 I've had a bit of an odd issue when I stop the xdebug listening client in PHPStorm, it seems that subsequent requests always times out because docker is reporting that host.docker.internal has port 9003 open when it's actually closed so the app always waits for the xdebug client.

I installed nmap on my webapp php container and host to test. If I run "nmap -p 9003 localhost" with the debug client running on my host I can see it open, after turning it off in PHPstorm the same scan shows that it's closed however running "nmap -p 9003 host.docker.internal" inside the container reports that it's still open. If I open other services on my host too it seems that ports start showing as open on the docker internal network however never report as closed after shutting them down on the host.

I upgraded to Docker 4.6.1 but the problem still persists.

Any advice would be appreciated.



Solution 1:[1]

This has been fixed in Docker 4.8.1 https://docs.docker.com/desktop/mac/release-notes/

Solution 2:[2]

UPDATE: Downgrading to Docker 4.5.0 resolved the issue.

This doesn't solve the problem, just helps to avoid restarting Docker while we are waiting for the fix. Make changes in xdebug.ini:

xdebug.start_with_request=trigger
xdebug.idekey=VSCODE

This tells XDebug to connect to debugger only if "trigger" is present in the HTTP request.

Now, install Chrome extension Xdebug helper, it's old but still works. Open extensions settings (chrome-extension://eadndfjplgieldjbigjakmdgkmoaaaoc/options.html) and set IDE key to "Other" "VSCODE".

Xdebug helper settings

Now, when you want to debug, you enable debugging in VSCode and also enable debugging in Chrome using that extension:

Enabling debugging in Xdebug helper

When you are done debugging - choose "Disable" in the extension, and PHP won't try to connect to your debugger, even if the port is still open. How it works - extension just sends cookie XDEBUG_SESSION=VSCODE with each request, and XDebug connects to the debugger only when this cookie is present.

P.S. You can replace VSCODE with IDE key that your IDE uses, or just any string.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 gkr34kg
Solution 2