问题
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?
回答1:
Yes, we can communicate between the containers running task.
Here is the system architecture
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
After that you can use curl
command override to container for testing:
回答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 toname: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.
回答3:
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).
来源:https://stackoverflow.com/questions/58196930/communication-between-containers-in-ecs-task-definition