Answers: <p>Understanding 1 is not correct, localhost is only for internal containers not other can access on localhost, if they share same network name can be used, nodeapp:3000 will work on local.</p>
  <p>For different services we have to call them by local IP or a domain name if not in same network.</p>
  
Answers: CC BY-SA 4.0
Answers: 2021-08-11T01:31:34.820Z
Answers: 2021-08-11T05:17:34.333Z
Answers: 2021-08-11T05:17:34.333Z
Answers: Siddique Ahmad
Body: <h2>Problem</h2>
  <p>How do ECS containers communicate between each others?</p>
  <h2>Situation</h2>
  <p>I use docker compose ecs integration.<br />
  In AWS docker context, <code>docker compose up</code> did a deploy correctly.<br />
  But reverse proxy doesn't work correctly, maybe because of <strong>communication with containers</strong>.</p>
  <p>My <code>docker-compose.yml</code></p>
  <pre><code>version: '3'
  services:
    nginx:
      image: {ACCOUNT}.dkr.ecr.{REGION}.amazonaws.com/{MY_IMAGE}
      ports:
        - &quot;80:80&quot;
  
    nodeapp:
      image: {ACCOUNT}.dkr.ecr.{REGION}.amazonaws.com/{MY_REACT_IMAGE}
      working_dir: /src
      command: sh -c &quot;yarn install &amp;&amp; yarn build &amp;&amp; yarn start&quot;
      ports:
        - &quot;3000:3000&quot;
  
  </code></pre>
  <p>MY nginx.conf</p>
  <pre><code>server {
      listen 80;
      server_name localhost;
      location / {
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header Host $http_host;
          proxy_set_header X-NginX-Proxy true;
          proxy_pass http://localhost:3000/;
          # proxy_pass http://nodeapp:3000/; // works only for local
          # proxy_pass http://nodeapp.{MY CLUSTER}.local:3000/; // doesn't work
  
      }
  }
  
  </code></pre>
  <h2>My understandings</h2>
  <ol>
  <li>In the same ECS service, containers can communicate with each other by <strong>localhost</strong>.</li>
  <li>In the diffrent ECS service, containers can communicate with each other by <strong>service discovery</strong>.</li>
  </ol>
  <p>Are these correct? And, how do I do that?</p>
  <h4>1: The same ECS service</h4>
  <p>I tried to do that at first. But, <code>docker compose up</code>(ecs integration) created different services(for <em>nginx service</em> and <em>application(react) service</em>. How do I configure that?</p>
  <h4>2: The different service</h4>
  <p>In nginx conf, <strong>proxy_pass http:nodeapp.{MY_CLUSTER}.local:3000</strong> doesn't work correctly.</p>
  <hr />
  <p>How do ECS containers communicate between each others?<br />
  Let me know the simplest way.<br />
  Thanks in advance.</p>
  
Tags: <docker><docker-compose><containers><fargate><ecs>
Title: How to communicate between AWS ECS containers
UserDisplayName: Omori
ThreadText: # How to communicate between AWS ECS containers
  
  (Asked by: username_0 on 2021-08-09)
  
  ## Problem
  
  How do ECS containers communicate between each others?
  
  ## Situation
  
  I use docker compose ecs integration.  
  In AWS docker context, `docker compose up` did a deploy correctly.  
  But reverse proxy doesn't work correctly, maybe because of **communication with containers**.
  
  My `docker-compose.yml`
  
  ```
  version: '3'
  services:
    nginx:
      image: {ACCOUNT}.dkr.ecr.{REGION}.amazonaws.com/{MY_IMAGE}
      ports:
        - "80:80"
  
    nodeapp:
      image: {ACCOUNT}.dkr.ecr.{REGION}.amazonaws.com/{MY_REACT_IMAGE}
      working_dir: /src
      command: sh -c "yarn install && yarn build && yarn start"
      ports:
        - "3000:3000"
  
  ```
  
  MY nginx.conf
  
  ```
  server {
      listen 80;
      server_name localhost;
      location / {
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header Host $http_host;
          proxy_set_header X-NginX-Proxy true;
          proxy_pass http://localhost:3000/;
          # proxy_pass http://nodeapp:3000/; // works only for local
          # proxy_pass http://nodeapp.{MY CLUSTER}.local:3000/; // doesn't work
  
      }
  }
  
  ```
  
  ## My understandings
  
  1. In the same ECS service, containers can communicate with each other by **localhost**.
  2. In the diffrent ECS service, containers can communicate with each other by **service discovery**.
  
  Are these correct? And, how do I do that?
  
  #### 1: The same ECS service
  
  I tried to do that at first. But, `docker compose up`(ecs integration) created different services(for *nginx service* and *application(react) service*. How do I configure that?
  
  #### 2: The different service
  
  In nginx conf, **proxy\_pass http:nodeapp.{MY\_CLUSTER}.local:3000** doesn't work correctly.
  
  ---
  
  How do ECS containers communicate between each others?  
  Let me know the simplest way.  
  Thanks in advance.
  
  **Tags:** docker, docker-compose, containers, fargate, ecs
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2021-08-11. Score: 2
  
  Understanding 1 is not correct, localhost is only for internal containers not other can access on localhost, if they share same network name can be used, nodeapp:3000 will work on local.
  
  For different services we have to call them by local IP or a domain name if not in same network.
  
  
  ---
  
  