How to communicate between containers in the same task in AWS ECS?

浪尽此生 提交于 2021-01-27 21:51:06

问题


I have a service-A with a task definition task-A that includes multiple container definitions, for example nginx and grafana. How can these containers communicate with each other? The network used is the default bridge network.

I have tried curl grafana:3000, but the container is not able to resolve the name. If I would try the same on my local machine it would work. What am I missing?

Here is my task definition:

resource "aws_ecs_task_definition" "this" {
  family = "x"
  execution_role_arn = "x"
  task_role_arn = "x"
  container_definitions = jsonencode(local.task_definition)
}

Container definition excerpt:

locals {
  task_definition = [
    {
      name: "nginx",
      image: "nginx:latest",
      portMappings: [{
        containerPort: 80,
        hostPort: 0,
        protocol: "tcp"
      }],
      dependsOn: [{
        "containerName": "grafana",
        "condition": "START"
      }]
    },
    {
      name: "grafana",
      image: "grafana/grafana:latest",
      portMappings: [{
        containerPort : 3000,
        hostPort: 0,
        protocol: "tcp"
      }]
    }
  ]
}

回答1:


dependsOn only work to start the container in order, you need linking to make service to service level communication between container.

      dependsOn: [{
        "containerName": "grafana",
        "condition": "START"
      }]
      "links": [
        "grafana"
      ]

From Doc

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

communicate-between-containers-in-the-same-task-in-aws-ecs



来源:https://stackoverflow.com/questions/62702348/how-to-communicate-between-containers-in-the-same-task-in-aws-ecs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!