title: Chrooted PHP-FPM script cannot resolve DNS after a moment
body: <p>I need some help to understand why a PHP-FPM chrooted PHP script fails to resolve an FQDN few instants after the PHP-FPM service started.</p>
  
  <p>When I (re)start the PHP-FPM service, it works (resolution succeeds) a few seconds and then resolution fails.</p>
  
  <p>I chrooted a PHP application (a WordPress actually) via PHP-FPM (settings `` chroot` in the PHP-FPM pool configuration file) and gave PHP what it needed:</p>
  
  <ul>
  <li>A basic <code>/etc/hosts</code> file</li>
  <li>A <code>mount --bind</code> of /etc/ssl/certs</li>
  <li>A <code>/dev/urandom</code> (via <code>mknod</code>)</li>
  <li>A <code>mount --bind</code> of /usr/share/zoneinfo</li>
  <li>A <code>mount --bind</code> of /var/run/mysqld for the socket to MySQL.</li>
  <li>A <code>mount --bind</code> of /var/run/nscd for the socket to nscd resolver.</li>
  <li>A place to store PHP sessions</li>
  </ul>
  
  <p>I noticed the issue when WordPress complained it could not download updates with:</p>
  
  <p><em>stream_socket_client(): php_network_getaddresses: getaddrinfo failed: Name or service not known stream_socket_client(): unable to connect to tcp://www.wordpress.org:80 (php_network_getaddresses: getaddrinfo failed: Name or service not known)</em></p>
  
  <p>Sample script:</p>
  
  <pre><code>&lt;?php
  $domain = 'www.example.com';
  echo 'gethostbynamel(): '; var_dump(gethostbynamel($domain));
  echo 'checkdnsrr(): ';     var_dump(checkdnsrr($domain, 'A'));
  echo 'dns_get_record(): '; var_dump(dns_get_record($domain));
  ?&gt;
  </code></pre>
  
  <p>When it works:</p>
  
  <pre><code>gethostbynamel(): array(1) {
    [0]=&gt;
    string(13) "93.184.216.34"
  }
  checkdnsrr(): bool(true)
  dns_get_record(): array(1) {
    [0]=&gt;
    array(5) {
      ["host"]=&gt;
      string(15) "www.example.com"
      ["class"]=&gt;
      string(2) "IN"
      ["ttl"]=&gt;
      int(86348)
      ["type"]=&gt;
      string(1) "A"
      ["ip"]=&gt;
      string(13) "93.184.216.34"
    }
  }
  </code></pre>
  
  <p>After a few instants:</p>
  
  <pre><code>gethostbynamel(): bool(false)
  checkdnsrr(): bool(true)
  dns_get_record(): array(1) {
    [0]=&gt;
    array(5) {
      ["host"]=&gt;
      string(15) "www.example.com"
      ["class"]=&gt;
      string(2) "IN"
      ["ttl"]=&gt;
      int(86400)
      ["type"]=&gt;
      string(1) "A"
      ["ip"]=&gt;
      string(13) "93.184.216.34"
    }
  }
  </code></pre>
  
  <p>Note that, in any case, both <code>dns_get_record()</code> and <code>checkdnsrr()</code> <strong>always</strong> works fine.</p>
  
  <p>Three questions:</p>
  
  <ol>
  <li>What makes <code>dns_get_record()</code> and <code>checkdnsrr()</code> achieve to resolve FQDN when <code>gethostbynamel()</code> fails?</li>
  <li>What does <code>gethostbynamel()</code> need in a chrooted setup?</li>
  <li>What could make it works only for a few moment?</li>
  </ol>
  
  <p>Software versions:</p>
  
  <ul>
  <li>Debian 9</li>
  <li>Nginx 10.3</li>
  <li>PHP 7.0.19</li>
  </ul>
  
  <p>Thanks</p>