title: Xdebug inside Docker on Mac M1 2021 is not working
body: <p>I ported a local development setup from Linux to a new Mac machine and am having problems with getting Xdebug to work with PhpStorm in a Mac M1 Pro machine.</p>
  <p>I have one container with PHP where Xdebug is installed and configured like this:</p>
  <pre class="lang-ini prettyprint-override"><code>zend_extension=/usr/lib/php8/modules/xdebug.so
  xdebug.mode=debug
  
  xdebug.idekey=PHPSTORM
  xdebug.client_port=9001
  xdebug.start_with_request=yes
  xdebug.discover_client_host=1
  xdebug.log=/var/log/xdebug.log
  </code></pre>
  <p>In PhpStorm I am listening to port 9001 (I use 9000 for other service).</p>
  <p>Here are Xdebug logs inside docker container at <code>/var/log/xdebug.log</code></p>
  <pre><code>/var/www/html/app # tail -f /var/log/xdebug.log 
  [49] [Step Debug] INFO: Checking header 'HTTP_X_FORWARDED_FOR'.
  [49] [Step Debug] INFO: Checking header 'REMOTE_ADDR'.
  [49] [Step Debug] INFO: Client host discovered through HTTP header, connecting to 172.18.0.4:9001.
  [49] [Step Debug] WARN: Creating socket for '172.18.0.4:9001', poll success, but error: Operation in progress (29).
  [49] [Step Debug] WARN: Could not connect to client host discovered through HTTP headers, connecting to configured address/port: localhost:9001. :-|
  [49] [Step Debug] WARN: Creating socket for 'localhost:9001', poll success, but error: Operation in progress (29).
  [49] [Step Debug] WARN: Creating socket for 'localhost:9001', connect: Address not available.
  [49] [Step Debug] ERR: Could not connect to debugging client. Tried: 172.18.0.4:9001 (from REMOTE_ADDR HTTP header), localhost:9001 (fallback through xdebug.client_host/xdebug.client_port) :-(
  [49] Log closed at 2022-03-15 16:28:46.957669
  
  </code></pre>
  <p>I don't understand why the connection is not happening/failing.</p>
  <p>Also I have a <code>docker-compose.yml</code> file that has the service API above as</p>
  <pre class="lang-yaml prettyprint-override"><code>  api:
      build:
        context: .
      expose:
        - 8080
      container_name: api
  </code></pre>
  <p><strong>What I tried already ?</strong></p>
  <ol>
  <li>Changed xdebug.ini config to use remote host <code>docker.for.mac.localhost</code>, then <code>host.docker.internal</code> but no success.</li>
  <li>Changed <code>start_with_request=trigger</code> instead of <code>yes</code>.</li>
  <li>Disabled Mac firewall thinking that maybe port 9001 cannot be used by Docker.</li>
  </ol>
  <p><strong>Update 19 August 2022 - This does not work debugging cli scripts (phpunit tests)</strong></p>
  <p>The provided answer with config below is working via HTTP requests but not for phpunit tests or cli  scripts.</p>
  <p>Content of <code>/etc/php8/conf.d/50_xdebug.ini</code></p>
  <pre><code>                                                                                 
  zend_extension=/usr/lib/php8/modules/xdebug.so
  xdebug.mode=debug
  
  xdebug.idekey=PHPSTORM
  xdebug.client_port=9001
  xdebug.start_with_request=yes
  xdebug.discover_client_host=0
  xdebug.client_host=host.docker.internal
  xdebug.log=/var/log/xdebug.log
  
  </code></pre>
  <p>Debugging does not work and I get the error below</p>
  <pre><code>Xdebug: [Step Debug] Could not connect to debugging client. Tried: 172.18.0.1:9001 (through xdebug.client_host/xdebug.client_port) :-(
  </code></pre>
  <p>When my editor PhpStorm tries to run this</p>
  <pre><code>[sshConfig://api-root@docker.dev:2023]:/usr/bin/php -dxdebug.mode=debug -dxdebug.client_port=9001 -dxdebug.client_host=172.18.0.1 /var/www/html/app/vendor/phpunit/phpunit/phpunit --configuration /var/www/html/app/phpunit.xml --filter &quot;/(Tests\\Unit\\App\\Services\\Maps\\GoogleMapsServiceTest::testGetPlaceFromId)( .*)?$/&quot; --test-suffix GoogleMapsServiceTest.php /var/www/html/app/tests/unit/app/Services/Maps --teamcity
  </code></pre>
  <p>And also the /var/log/xdebug.log logs inside the docker machine</p>
  <pre><code>[326] [Step Debug] INFO: Connecting to configured address/port: 172.18.0.1:9001.
  [326] [Step Debug] WARN: Creating socket for '172.18.0.1:9001', poll success, but error: Operation in progress (29).
  [326] [Step Debug] ERR: Could not connect to debugging client. Tried: 172.18.0.1:9001 (through xdebug.client_host/xdebug.client_port) :-(
  [326] Log closed at 2022-08-19 16:24:59.855397
  
  </code></pre>
  <p>I fixed this with a hacky way by modifying phpunit interpreter configuration. I am not sure why the <code>/etc/php8/conf.d/50_xdebug.ini</code> value set to internal docker host was ignored in this case</p>
  <p><a href="https://i.stack.imgur.com/ptr8d.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ptr8d.png" alt="enter image description here" /></a></p>