title: Docker Swarm - load-balancing to closest node first
body: <p>I'm trying to optimize Docker-Swarm load-balancing in a way that it will first route requests to services by the following priority</p>
  
  <ol>
  <li>Same machine </li>
  <li>Same DC </li>
  <li>Anywhere else.</li>
  </ol>
  
  <p>Given the following setup:</p>
  
  <ul>
  <li>DataCenter-I
  
  <ul>
  <li>Server-I
  
  <ul>
  <li>Nginx:80</li>
  </ul></li>
  <li>Server-II
  
  <ul>
  <li>Nginx:80</li>
  <li>Worker</li>
  </ul></li>
  </ul></li>
  <li>DataCenter-II
  
  <ul>
  <li>Server-I
  
  <ul>
  <li>Nginx:80</li>
  <li>Worker</li>
  </ul></li>
  </ul></li>
  </ul>
  
  <p>In case and DataCenter-I::Server-II::Worker will issue an API request over port 80, The desired behavior is:</p>
  
  <ul>
  <li>Check if there are any tasks (containers) mapped to port:80 on local server (DataCenter-I::Server-II)</li>
  <li>Fallback and check in local DataCenter (i.e DataCenter-I::Server-I)</li>
  <li>Fallback and check in all clusters (i.e DataCenter-II::Server-I)</li>
  </ul>
  
  <p>This case is very useful when using workers and response time doesn't matter while bandwidth does.</p>
  
  <p>Please advise,
  Thanks!</p>