'Communication between containers in ECS task definition

I have a task definition in ECS running in awsvpc mode, containing 2 docker containers. My question is how can I communicate between containers in a task definition. Do they act similar to docker-compose?



Solution 1:[1]

Multiple containers within a task in awsvpc networking mode will share the task ENI and the network namespace, so they can communicate to each other using localhost (or the equivalent 127.0.0.1 loopback IP address).

Solution 2:[2]

The linking concept is only valid in case of AWS ec2 type service, you can not use linking in awsvpc network mode. linking between task is only allowed in that container which is part of the same task definition, it mean you should run two containers in the same task definition to create linking which similar to docker-compose.

links
Type: string array

Required: no

The link parameter allows containers to communicate with each other without the need for port mappings. Only supported if the network mode of a task definition is set to bridge. The name:internalName construct is analogous to name:alias in Docker links. Up to 255 letters (uppercase and lowercase), numbers, hyphens, and underscores are allowed.

Note

This parameter is not supported for Windows containers or tasks using the awsvpc network mode.

container-linking-in-task-definition

You can use service discovery in case of AWS VPC a recommended approach by AWS incase of AWSvpc mood.

Solution 3:[3]

Yes, we can communicate between the containers running task.

Here is the system architecture

enter image description here

And we will use Service discovery for register to Service task (Fargate/EC2 task)

This is an example how to launch one service to ECS with CloudFormation

...
Resources:

    DiscoveryService:
        Type: AWS::ServiceDiscovery::Service
        Properties:
          DnsConfig:
            RoutingPolicy: MULTIVALUE
            DnsRecords:
              - TTL: 60
                Type: A
              - TTL: 60
                Type: SRV
          HealthCheckCustomConfig:
            FailureThreshold: 1
          Name: !Ref ServiceName
          NamespaceId: !Ref PrivateNamespaceId // Example: ns-foobar
...
    Service:
        Type: AWS::ECS::Service
        DependsOn: LoadBalancerRule
        Properties:
            Cluster: !Ref ClusterName
            LaunchType: "FARGATE"
            DesiredCount: !Ref DesiredCount
            ...
            ServiceRegistries:
                - RegistryArn: !GetAtt DiscoveryService.Arn
                  Port: 80 // Container port and service port

Here is the result

enter image description here

After that you can use curl command override to container for testing:

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Raoni
Solution 2 Adiii
Solution 3 Quynh Nguyen