0

Problem

How do ECS Fargate 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.

Omori
  • 33
  • 1
  • 6

1 Answers1

0

The Docker documentation defines how this works nicely.

https://docs.docker.com/cloud/ecs-integration/#service-discovery

Your Understandings

  1. No, containers will not communicate with each other via localhost. Containers are separate entities that separate running processes from the host process. Unless started with host networking, they do not have access to the host OS, and thus can not see each other on their own loopback address.
  2. When containers are deployed via docker-compose, AWS sets up the necessary DNS to allow your containers to connect in the same way they usually do (Through service name), as well as via <service>.<compose_project_name>.local. It may take some time for this DNS to be setup and resolve, so your app needs to be able to handle backoffs

It looks like you have got the right idea with nodeapp.{MY_CLUSTER}.local:3000. You should include the error logs you are seeing with your deployment in order to help you further.

It is important to remember to specify the your compose file as version 3.8 for full support with AWS

Similar Questions

It's worth noting, that this question has been asked before, and has appears easily when you search for the answers:

Getting containers to talk to each other in Elastic Container Service (ECS)?

TheQueenIsDead
  • 865
  • 5
  • 19
  • It's worth noting that in ECS by itself (when not using docker-compose integrations for example) that containers within a task _do_ communicate with each other over the localhost address. It's the `awsvpc` mode of networking where they all share a single elastic address – Dockstar May 10 '23 at 14:19