Answers: <p>Yes Felix. Loosely speaking it &quot;load balance&quot; across your 5 web container instances. If you are using the ECS / Compose integration and you are bringing up this stack with a <code>compose</code> up this is roughly what happens:</p>
  <ul>
  <li>it creates an ECS cluster</li>
  <li>it creates task def for the <code>web</code> task and the <code>nginx</code> task</li>
  <li>it creates a service for <code>web</code> (with 5 tasks in it)</li>
  <li>it creates a service for <code>nginx</code> (with 1 task in it)</li>
  <li>it exposes the <code>nginx</code> service via an ALB to the public (because you expose the port 80)</li>
  <li>it creates an internal mechanism based on ECS Service Discovery to be able to resolve <code>web</code> inside the cluster (that's how the <code>nginx</code> is able to reach an entity called <code>web</code>).</li>
  </ul>
  <p>ECS SD leverages CloudMap/Route53 which will round-robin across the 5 entries (<code>web</code> tasks). You can read more <a href="https://docs.amazonaws.cn/en_us/AmazonECS/latest/developerguide/service-discovery.html" rel="nofollow noreferrer">here</a>.</p>
  
Answers: CC BY-SA 4.0
Answers: 2021-05-14T13:13:48.467Z
Answers: 2021-05-14T13:13:48.467Z
Answers: mreferre
Body: <p>Below is a simplified version of my <code>docker-compose.yml</code> file.</p>
  <p>I can understand or find any resources to tell me which container of the 5 <code>django</code> replicas the nginx container will target if i reference it.</p>
  <p>For example, as you can see if my compose file I have one instance of Nginx and five of Django.</p>
  <p>If my nginx config i have an upstream like this:</p>
  <pre><code>upstream docking_django {
      server web:3000;
  }
  </code></pre>
  <p>Which web container will the upstream come from? Will it load balance between them?
  I'm deploying to AWS ECS and when I try running it, it seems like it's load balancing somehow. But I would like to understand it further since it doesn't seem to behave the same way always.</p>
  <pre><code>version: '3.9'
  
  services:
    web:
      image: .dkr.ecr.eu-west-3.amazonaws.com/tin-api-v2/django:${IMAGE_TAG}
      build:
        context: .
        dockerfile: ./Dockerfile
      networks: 
        - demoapp
      deploy:
        replicas: 5
  
    nginx:
      image: .dkr.ecr.eu-west-3.amazonaws.com/tin-api-v2/nginx:${IMAGE_TAG}
      build:
        context: .
        dockerfile: ./nginx.Dockerfile
      ports:
        - 80:80
      depends_on:
        - web
      networks: 
        - demoapp
      deploy:
        replicas: 1
  
  networks:
    demoapp:
  </code></pre>
  
Tags: <docker><nginx><amazon-ecs>
Title: Which container will be targeted when replicas larger than 1
UserDisplayName: Felix Eklöf
ThreadText: # Which container will be targeted when replicas larger than 1
  
  (Asked by: username_0 on 2021-05-14)
  
  Below is a simplified version of my `docker-compose.yml` file.
  
  I can understand or find any resources to tell me which container of the 5 `django` replicas the nginx container will target if i reference it.
  
  For example, as you can see if my compose file I have one instance of Nginx and five of Django.
  
  If my nginx config i have an upstream like this:
  
  ```
  upstream docking_django {
      server web:3000;
  }
  
  ```
  
  Which web container will the upstream come from? Will it load balance between them?
  I'm deploying to AWS ECS and when I try running it, it seems like it's load balancing somehow. But I would like to understand it further since it doesn't seem to behave the same way always.
  
  ```
  version: '3.9'
  
  services:
    web:
      image: .dkr.ecr.eu-west-3.amazonaws.com/tin-api-v2/django:${IMAGE_TAG}
      build:
        context: .
        dockerfile: ./Dockerfile
      networks: 
        - demoapp
      deploy:
        replicas: 5
  
    nginx:
      image: .dkr.ecr.eu-west-3.amazonaws.com/tin-api-v2/nginx:${IMAGE_TAG}
      build:
        context: .
        dockerfile: ./nginx.Dockerfile
      ports:
        - 80:80
      depends_on:
        - web
      networks: 
        - demoapp
      deploy:
        replicas: 1
  
  networks:
    demoapp:
  
  ```
  
  **Tags:** docker, nginx, amazon-ecs
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2021-05-14. Score: 1 | Accepted answer
  
  Yes username_0. Loosely speaking it "load balance" across your 5 web container instances. If you are using the ECS / Compose integration and you are bringing up this stack with a `compose` up this is roughly what happens:
  
  * it creates an ECS cluster
  * it creates task def for the `web` task and the `nginx` task
  * it creates a service for `web` (with 5 tasks in it)
  * it creates a service for `nginx` (with 1 task in it)
  * it exposes the `nginx` service via an ALB to the public (because you expose the port 80)
  * it creates an internal mechanism based on ECS Service Discovery to be able to resolve `web` inside the cluster (that's how the `nginx` is able to reach an entity called `web`).
  
  ECS SD leverages CloudMap/Route53 which will round-robin across the 5 entries (`web` tasks). You can read more [here](https://docs.amazonaws.cn/en_us/AmazonECS/latest/developerguide/service-discovery.html).
  
  
  ---
  
  