如何在 AWS ECS 中的同一任务中的容器之间进行通信?

tra*_*nag 3 amazon-ecs docker

我有一个包含多个容器定义service-A的任务定义task-A,例如nginxgrafana。这些容器如何相互通信?使用的网络是默认的桥接网络。

我试过了curl grafana:3000,但容器无法解析名称。如果我在本地机器上尝试相同的方法,它会起作用。我错过了什么?

这是我的任务定义:

resource "aws_ecs_task_definition" "this" {
  family = "x"
  execution_role_arn = "x"
  task_role_arn = "x"
  container_definitions = jsonencode(local.task_definition)
}
Run Code Online (Sandbox Code Playgroud)

容器定义摘录:

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"
      }]
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

Adi*_*iii 5

dependsOn 只能按顺序启动容器,您需要链接以在容器之间进行服务到服务级别的通信。

      dependsOn: [{
        "containerName": "grafana",
        "condition": "START"
      }]
      "links": [
        "grafana"
      ]
Run Code Online (Sandbox Code Playgroud)

来自文档

links
Type: string array

Required: no
Run Code Online (Sandbox Code Playgroud)

link 参数允许容器相互通信而无需端口映射。仅当任务定义的网络模式设置为桥接时才支持。name:internalName 构造类似于 Docker 链接中的 name:alias。最多 255 个字母(大写和小写)、数字、连字符和下划线

aws-ecs 中相同任务中的容器间通信