title: Docker Cannot assign requested address
body: <p>I think I'm pretty close (I hope) to having xdebug running in a docker container, aiming to connect via Visual Studio Code.</p>
  <p>I think that maybe I'm supposed to add a config to the <code>/etc/hosts</code> file in the container, directing an IP address to the url my files are being served over, but am not sure what that IP address is supposed to correspond to.</p>
  <p>The error:</p>
  <h3>via <code>xdebug_info()</code> (and in <code>/tmp/xdebug.log</code>):</h3>
  <pre><code>[Step Debug] Could not connect to debugging client. Tried: localhost:9003 (through xdebug.client_host/xdebug.client_port) :-(
  </code></pre>
  <h3>via <code>telnet localhost 9003</code>:</h3>
  <pre><code>Trying 127.0.0.1...
  Trying ::1...
  telnet: Unable to connect to remote host: Cannot assign requested address
  </code></pre>
  <h3>The <code>xdebug.ini</code> file:</h3>
  <pre><code>zend_extension=xdebug.so
  
  [xdebug]
  xdebug.mode=debug
  xdebug.start_with_request = yes
  xdebug.discover_client_host = 0
  xdebug.remote_connect_back = 1
  xdebug.client_port = 9003
  xdebug.force_error_reporting = 1
  xdebug.remote_host=host.docker.internal
  xdebug.idekey=VSCODE
  xdebug.mode = debug
  xdebug.log=/tmp/xdebug-local.log
  </code></pre>
  <h3>The <code>launch.json</code>:</h3>
  <pre><code>  &quot;version&quot;: &quot;0.2.0&quot;,
    &quot;configurations&quot;: [
      {
        &quot;type&quot;: &quot;php&quot;,
        &quot;request&quot;: &quot;launch&quot;,
        &quot;name&quot;: &quot;xDebug listen&quot;,
        &quot;port&quot;: 9003,
        &quot;url&quot;: &quot;http://mzmbo.test&quot;,
        &quot;stopOnEntry&quot;: true,
        &quot;pathMappings&quot;: {
          &quot;/var/www/html/wp-content/plugins/my-wp-plugin&quot;: &quot;${workspaceRoot}/my-wp-plugin&quot;
        }
      }
    ]
  }
  </code></pre>
  <h3>The <code>Dockerfile</code>:</h3>
  <pre><code>FROM &quot;wordpress:${WP_VERSION:-latest}&quot;
  
  RUN apt-get update -y \
    &amp;&amp; apt-get install -y \
        libxml2-dev \
        vim \
        lsof \
        ufw \
        telnet \
    &amp;&amp; apt-get clean -y \
    &amp;&amp; ufw allow 9003 \
    &amp;&amp; docker-php-ext-install soap  \
    &amp;&amp; docker-php-ext-enable soap \
    &amp;&amp; pecl install xdebug \
    &amp;&amp; docker-php-ext-enable xdebug
  
  # Replace php.ini
  COPY php.ini /usr/local/etc/php
  COPY docker-php-ext-xdebug.ini /usr/local/etc/php/conf.d
  </code></pre>
  <h3>Finally the <code>docker-compose.yml</code>:</h3>
  <pre><code>version: &quot;3&quot;
  
  services:
    wordpress:
      build: .
      environment:
        VIRTUAL_HOST: &quot;myproject.test&quot;
        WORDPRESS_DB_HOST: &quot;mysql&quot;
        WORDPRESS_DB_NAME: &quot;wordpress&quot;
        WORDPRESS_DB_PASSWORD: &quot;&quot;
        WORDPRESS_DB_USER: &quot;root&quot;
      depends_on:
        - &quot;mysql&quot;
      networks:
        - &quot;front&quot;
        - &quot;back&quot;
      volumes:
        - &quot;wp:/var/www/html:rw&quot;
        - &quot;./my-wp-plugin:/var/www/html/wp-content/plugins/my-wp-plugin:ro&quot;
  </code></pre>
  <h2>Result of <code>xdebug_info()</code> shows:</h2>
  <pre><code>Step Debugger   ✔ enabled
  xdebug.client_host  localhost
  xdebug.client_port  9003
  xdebug.start_with_request   yes
  </code></pre>
  <p>I bet I need to add a configuration, but not sure what.</p>
  <h1>Update</h1>
  <p>From <a href="https://veselin.dev/blog/xdebug-3-in-docker" rel="nofollow noreferrer">this tutorial</a>, I see that <code>xdebug.discover_client_host=true</code> &quot;tells Xdebug to attempt to extract the IP of the client from the HTTP request&quot;, falling back to <code>client_host</code> if that fails. (It checks <code>$_SERVER['HTTP_X_FORWARDED_FOR']</code> and <code>$_SERVER['REMOTE_ADDR']</code> variables to find out which IP address to use.) This is the config that replaces xDebug 2's <code>remote_connect_back</code>. Most of xDebug's <a href="https://xdebug.org/docs/upgrade_guide" rel="nofollow noreferrer">significant configs have changed</a> between <code>v2</code> and <code>v3</code>.</p>
  <p>Still getting the same connection errors, though:</p>
  <pre><code>Trying 192.168.16.1...
  telnet: Unable to connect to remote host: Connection refused
  </code></pre>
  <p>And in the logs:</p>
  <pre><code>[21] [Step Debug] WARN: Could not connect to client host discovered \
   through HTTP headers, connecting to configured address/port: localhost:9003. :-|
  [21] [Step Debug] WARN: Creating socket for 'localhost:9003', \ 
  poll success, but error: Operation now in progress (29).192.168.16.1:9003 (from HTTP_X_FORWARDED_FOR HTTP header), \
   localhost:9003 (fallback through xdebug.client_host/xdebug.client_port) :-(
  </code></pre>
  <p>I tried adding an <code>/etc/hosts</code> entry pointing <code>192.168.16.1</code> to <code>myproject.test</code> (in the Docker container) but haven't figured out how to flush the DNS records in the container. Maybe I need to add <a href="https://stackoverflow.com/a/58994501/2223106">extra host mapping</a> in the container?</p>
  <p>Came across a post that suggested <a href="https://docs.lando.dev" rel="nofollow noreferrer">Lando</a>, which appears to wrap Docker, can ease some of this pain, but I'm not ready to give up on this yet, so thank you very much if you've read this far.</p>
  <h1>Solution (gratefully) accepted below.</h1>
  <p>I also needed to make a change to the VS Code <code>launch.json</code> file:</p>
  <pre><code>&quot;stopOnEntry&quot;: false
  </code></pre>
  <p>Because I don't have the WP codebase in my repo, just a specific plugin. With <code>&quot;stopOnEntry&quot;: true</code>, the xDebug plugin was looking for an (non-existent) <code>index.php</code> file (starting point) in the <code>&quot;${workspaceRoot}</code>, as opposed to just checking for breakpoints, which is what I required, my local codebase just being <em>part</em> of the entire application.</p>
  <p>Also, I <em>think</em> that the <code># comments</code> in the accepted answer were invalidating the <code>.ini</code> file, so watch out for that if it seems like your settings aren't coming up.</p>