title: Simple docker-compose with two services: nginx and php
body: <p>I'm beginning with <a href="https://en.wikipedia.org/wiki/Docker_%28software%29" rel="nofollow noreferrer">Docker</a> and <a href="http://en.wikipedia.org/wiki/Nginx" rel="nofollow noreferrer">nginx</a>, and I'm trying to setup a two container environment running:</p>
  
  <ul>
  <li><code>nginx:latest</code> on one side</li>
  <li><code>php:fpm</code> on the other side</li>
  </ul>
  
  <p>I'm having trouble with php-fpm: I always get a <strong>502 Bad Gateway</strong> error.</p>
  
  <p>My setup is straightforward (<code>$TEST_DIR</code> is my working directory).</p>
  
  <p>My Docker compose config <code>TEST_DIR/docker-compose.yml</code>:</p>
  
  <pre><code>nginx:
      image: nginx
      ports:
          - "8080:80"
      volumes:
          - ./www:/usr/share/nginx/html
          - ./conf/nginx.conf:/nginx.conf
          - ./logs/nginx:/var/log/nginx
      links:
          - php:php
      command: nginx -c /nginx.conf
  
  php:
      image: php:fpm
      ports:
          - "9000:9000"
      volumes:
          - ./www:/var/www/html
  </code></pre>
  
  <p>The nginx config <code>$TEST_DIR/conf/nginx.conf</code>:</p>
  
  <pre><code>user  nginx;
  worker_processes  1;
  pid        /var/run/nginx.pid;
  
  events {
      worker_connections  2048;
      multi_accept on;
      use epoll;
  }
  
  http {
  
      log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                        '$status $body_bytes_sent "$http_referer" '
                        '"$http_user_agent" "$http_x_forwarded_for"';
  
      access_log  /var/log/nginx/access.log  main;
  
      server_tokens off;
      sendfile on;
      tcp_nopush on;
      tcp_nodelay on;
      keepalive_timeout 15;
      types_hash_max_size 2048;
      include /etc/nginx/mime.types;
      default_type application/octet-stream;
      access_log off;
      error_log off;
      gzip on;
      gzip_disable "msie6";
      open_file_cache max=100;
  
  
      upstream php-upstream {
          server php:9000;
      }
  
      server {
          listen       80;
          server_name  localhost;
  
          location / {
              root   /usr/share/nginx/html;
              index  index.html index.htm;
          }
  
          # Pass PHP scripts to PHP-FPM
          location ~* \.php$ {
              fastcgi_pass    php-upstream;
  
              include         fastcgi_params;
              fastcgi_param   SCRIPT_FILENAME    /var/www/html$fastcgi_script_name;
              fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;
              fastcgi_param   HTTPS off;
          }
  
          error_log  /var/log/nginx/php_error.log;
          access_log  /var/log/nginx/php_access.log;
      }
  }
  
  daemon off;
  </code></pre>
  
  <p>Then, I put my PHP content in the same directory as my <code>docker-compose.yml</code>:</p>
  
  <p><code>$TEST_DIR/www/test.php</code></p>
  
  <pre><code>&lt;?php phpinfo(); ?&gt;
  </code></pre>
  
  <p>If I start the infrastructure using <code>docker-compose up</code> and then go to <code>localhost:8080/test.php</code>, then I get the <strong>502 Bad Gateway</strong>
  and the following error from nginx:</p>
  
  <pre><code>[error] 6#6: *1 connect() failed (113: No route to host) while connecting to upstream, client: 172.17.42.1, server: localhost, request: "GET /phpinsfo2.php HTTP/1.1", upstream: "fastcgi://172.17.0.221:9000", host: "localhost:8080"
  </code></pre>
  
  <p>What is causing the error?</p>