title: IDE debugging with Docker, nginx, PHP7-FPM and Xdebug
body: <p>I'm currently preparing a development stack on macOS with <code>docker-compose</code> to be able to use <strong>Xdebug</strong> (Port: 9009) on <strong>PHP7-FPM</strong> (Port: 9000) and <strong>nginx</strong> (Port: 80) server.</p>
  
  <p>Apparently the configuration is OK, but I am not able to debug through the IDE.</p>
  
  <p><strong>Here's my settings:</strong></p>
  
  <p>My <code>.env</code> file:</p>
  
  <pre><code>APP_NAME=testeXdebug
  HOST_SERVER_NAME=myapp
  HOST_IP=docker.for.mac.localhost
  # Use docker.for.mac.localhost - for OS X
  # Use docker.for.win.localhost - for Windows
  </code></pre>
  
  <p>The <code>Dockerfile</code> PHP7-FPM + Xdebug:</p>
  
  <pre><code>FROM php:7.2-fpm
  
  RUN pecl install xdebug \
      &amp;&amp; docker-php-ext-enable xdebug
  </code></pre>
  
  <p>The <code>docker-compose.yml</code> file:</p>
  
  <pre><code>version: '3.5'
  
  services:
    web:
      image: nginx:1.15.2
      ports:
        - '80:80'
      volumes:
        - '.:/usr/share/nginx/html'
        - './config/default.conf:/etc/nginx/conf.d/default.conf'
        - '/tmp/${APP_NAME}/web:/var/log/nginx'
      env_file:
        - '.env'
      depends_on:
        - 'php-fpm'
      links:
        - 'php-fpm'
  
    php-fpm:
      build: './docker'
      ports:
        - '9000:9000'
        - '9009:9009'
      expose:
        - 9000
        - 9009
      volumes:
        - '.:/usr/share/nginx/html'
        - './config/php-dev.ini:/usr/local/etc/php/conf.d/php-dev.ini'
        - '/tmp/${APP_NAME}/php-fpm:/var/log/xdebug'
      environment:
        XDEBUG_CONFIG: "remote_host=${HOST_IP}"
        PHP_IDE_CONFIG: "serverName=${HOST_SERVER_NAME}"
      env_file:
        - '.env'
  </code></pre>
  
  <p>The <code>php-dev.ini</code> file:</p>
  
  <pre><code>; Xdebug
  xdebug.default_enable = 1
  xdebug.remote_enable = 1
  xdebug.remote_autostart = 1
  xdebug.remote_connect_back = 1
  xdebug.remote_port = 9009
  xdebug.profiler_enable = 0
  xdebug.idekey = PHPSTORM
  xdebug.remote_handler = dbgp
  xdebug.remote_mode = req
  xdebug.remote_log = /var/log/xdebug/xdebug.log
  </code></pre>
  
  <p>The nginx <code>default.conf</code> file:</p>
  
  <pre><code>server {
      listen          80;
      server_name     myapp;
      root            /usr/share/nginx/html;
      index           index.php index.html index.htm;
  
      location / {
          try_files $uri $uri/ /index.php?$query_string;
      }
  
      location ~ \.php$ {
          fastcgi_pass    php-fpm:9000;
          fastcgi_index   index.php;
          fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
          include         fastcgi_params;
      }
  
      location ~ /\.ht {
          deny  all;
      }
  }
  </code></pre>
  
  <hr>
  
  <p>Then when accessing the server through the browser with <a href="https://chrome.google.com/webstore/detail/xdebug-helper/eadndfjplgieldjbigjakmdgkmoaaaoc" rel="nofollow noreferrer">Xdebug helper</a> extension active:</p>
  
  <p><a href="https://i.stack.imgur.com/Sn2md.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Sn2md.png" alt="enter image description here"></a></p>
  
  <p>I get this debug log (xdebug):</p>
  
  <pre><code>Log opened at 2018-08-11 19:22:53
  I: Checking remote connect back address.
  I: Checking header 'HTTP_X_FORWARDED_FOR'.
  I: Checking header 'REMOTE_ADDR'.
  I: Remote address found, connecting to 192.168.128.1:9009.
  I: Connected to client. :-)
  -&gt; &lt;init xmlns="urn:debugger_protocol_v1" xmlns:xdebug="http://xdebug.org/dbgp/xdebug" fileuri="file:///usr/share/nginx/html/index.php" language="PHP" xdebug:language_version="7.2.8" protocol_version="1.0" appid="9" idekey="PHPSTORM"&gt;&lt;engine version="2.6.1"&gt;&lt;![CDATA[Xdebug]]&gt;&lt;/engine&gt;&lt;author&gt;&lt;![CDATA[Derick Rethans]]&gt;&lt;/author&gt;&lt;url&gt;&lt;![CDATA[http://xdebug.org]]&gt;&lt;/url&gt;&lt;copyright&gt;&lt;![CDATA[Copyright (c) 2002-2018 by Derick Rethans]]&gt;&lt;/copyright&gt;&lt;/init&gt;
  
  -&gt; &lt;response xmlns="urn:debugger_protocol_v1" xmlns:xdebug="http://xdebug.org/dbgp/xdebug" status="stopping" reason="ok"&gt;&lt;/response&gt;
  
  Log closed at 2018-08-11 19:22:53
  </code></pre>
  
  <p>nginx access log:</p>
  
  <pre><code>192.168.128.1 - - [11/Aug/2018:18:57:25 +0000] "GET /favicon.ico HTTP/1.1" 200 94197 "http://docker.for.mac.localhost/index.php?XDEBUG_SESSION_START=PHPSTORM" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36" "-"
  192.168.128.1 - - [11/Aug/2018:19:22:53 +0000] "GET /index.php?XDEBUG_SESSION_START=PHPSTORM HTTP/1.1" 200 94341 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36" "-"
  192.168.128.1 - - [11/Aug/2018:19:22:53 +0000] "GET /favicon.ico HTTP/1.1" 200 94205 "http://docker.for.mac.localhost/index.php?XDEBUG_SESSION_START=PHPSTORM" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36" "-"
  192.168.128.1 - - [11/Aug/2018:19:30:12 +0000] "GET /_intellij_phpdebug_validator.php HTTP/1.1" 200 516 "-" "Java/1.8.0_152-release" "-"
  </code></pre>
  
  <hr>
  
  <p>And setup VSCode with debug extension with this:</p>
  
  <pre><code>{
      "version": "0.2.0",
      "configurations": [
          {
              "name": "Listen for XDebug",
              "type": "php",
              "request": "launch",
              "port": 9009,
              "pathMappings": {
                  "/usr/share/nginx/html": "${workspaceRoot}"
              }
          }
      ]
  }
  </code></pre>
  
  <p>Add some break points:</p>
  
  <p><a href="https://i.stack.imgur.com/aku4G.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/aku4G.png" alt="enter image description here"></a></p>
  
  <p>And on <strong>PhpStorm</strong>:</p>
  
  <p><a href="https://i.stack.imgur.com/UlqwF.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/UlqwF.png" alt="enter image description here"></a></p>
  
  <p>PhpStorm CLI Interpreter using docker-compose:</p>
  
  <p><a href="https://i.stack.imgur.com/a2B5y.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/a2B5y.png" alt="enter image description here"></a></p>
  
  <p>But at <strong>Start listening for PHP Debug connection</strong> I get <code>Port 9009 is busy</code>.</p>
  
  <p>And IDE never start the debug tool... =(<br>
  What could I be missing out on? Please help me!</p>
  
  <hr>
  
  <p>After some comments:</p>
  
  <p>When removing <code>ports</code> and <code>expose</code> settings of <strong>php-fpm</strong> service from <code>docker-compose.yml</code></p>
  
  <pre><code>thiago@MA-TPR testeXdebug $ docker ps
  CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS              PORTS                NAMES
  735fc48fad63        nginx:1.15.2          "nginx -g 'daemon of…"   4 minutes ago       Up 4 minutes        0.0.0.0:80-&gt;80/tcp   testexdebug_web_1
  b9b16af98fb5        testexdebug_php-fpm   "docker-php-entrypoi…"   4 minutes ago       Up 4 minutes        9000/tcp             testexdebug_php-fpm_1
  thiago@MA-TPR testeXdebug $ 
  </code></pre>
  
  <p>And get this log from xdebug:</p>
  
  <pre><code>Log opened at 2018-08-12 00:56:39
  I: Checking remote connect back address.
  I: Checking header 'HTTP_X_FORWARDED_FOR'.
  I: Checking header 'REMOTE_ADDR'.
  I: Remote address found, connecting to 192.168.160.1:9009.
  W: Creating socket for '192.168.160.1:9009', poll success, but error: Operation now in progress (29).
  E: Could not connect to client. :-(
  Log closed at 2018-08-12 00:56:39
  </code></pre>
  
  <p>Executing <code>nc</code> from PHP container:</p>
  
  <pre><code>root@b9b16af98fb5:/var/www/html# nc -zv docker.for.mac.localhost 9009
  Warning: inverse host lookup failed for 192.168.65.2: Unknown host
  docker.for.mac.localhost [192.168.65.2] 9009 (?) open
  root@b9b16af98fb5:/var/www/html# 
  </code></pre>