Home > Software engineering >  How to do sidecar container communication in an ECS task?
How to do sidecar container communication in an ECS task?

Time:08-04

I have an ECS task where I have the main container and a sidecar container. I'm creating the task on EC2 and the network mode is bridge. My main container needs to talk to the sidecar container. But I am unable to do so.

My task definition is:

[
     {
       "name": "my-sidecar-container",
       "image": "ECR image name",
       "memory": "256",
       "cpu": "256",
       "essential": true,
       "portMappings": [
         {
           "containerPort": "50051",
           "hostPort": "50051",
           "protocol": "tcp"
         }
       ],
       "links": [
         "app"
       ]
     },
     {
       "name": "app",
       "image": "<app image URL here>",
       "memory": "256",
       "cpu": "256",
       "essential": true
     }
]

The sidecar is a gRPC server. To check if I can list all the gRPC endpoints if I do the following from my main app container, it does not work.

root@my-main-app# ./grpcurl -plaintext localhost:50051 list
Failed to dial target host "localhost:50051": dial tcp 127.0.0.1:50051: connect: connection refused

But if I mention the EC2 private IP, it works. e.g.

root@my-main-app# ./grpcurl -plaintext 10.0.56.69:50051 list

grpc.reflection.v1alpha.ServerReflection
health.v1.Health
server.v1.MyServer

So it is definitely a networking issue. Wondering how to fix it!

CodePudding user response:

If you're using bridge mode and linking, then you actually need to use link name as the address, instead of localhost. You would need to link the sidecar container to the app container (you are currently doing the opposite) and then use the sidecar's link name as the address.

If you were using awsvpc mode, then you would use localhost:containerport to communicate between containers in the same task.

  • Related