title: Docker load balance using NGINX proxy
body: <p>I'm trying to load balance an API server using nginx and docker's native  DNS.</p>
  
  <p>I was hoping nginx will round-robin API calls to all available servers. But even when I specify docker's DNS server as the resolver nginx forward the request to only one server.</p>
  
  <p>Relevant section from docker-compose.yml</p>
  
  <pre><code>proxy:
    restart: always
    build: ./src/nginx/.
    ports:
      - "8080:8080"
    links:
      - api:servers.api
  </code></pre>
  
  <p>nginx.conf</p>
  
  <pre><code>worker_processes 2;
  
  events { worker_connections 1024; }
  
  http {
      sendfile on;
  
      server {
          listen 8080;
  
          location / {
              resolver_timeout 30s;
              resolver 127.0.0.11 ipv6=off valid=10s;
              set $backend http://servers.api:80;
              proxy_pass          $backend;
              proxy_redirect      off;
          }
      }
  }
  </code></pre>
  
  <p>NGINX round-robin load balancer works if I manually specify each server, which I don't want to do since it can't scale automatically.</p>
  
  <pre><code>worker_processes 2;
  
  events { worker_connections 1024; }
  
  http{
      sendfile on;
  
      upstream api_servers{
          server project_api_1:80;
          server project_api_2:80;
          server project_api_3:80;
      }
  
      server{
          listen 8080;
  
          location / {
              proxy_pass          http://api_servers;
              proxy_redirect      off;
          }
      }
  }
  </code></pre>
  
  <p>How to configure nginx in such a way that it can detect new containers added and include them in the round-robin?</p>