如何在 AWS ECS 任务中 link 2 个容器 运行

How do I link 2 containers running in a AWS ECS task

我是 ECS 的新手,我正在尝试使用 Fargate 在 ECS 任务中部署几个容器。

我有 1 个容器 运行 使用 Angular2 并且在 nginx 上是 运行,另一个容器是后端并且在 Springboot 上是 运行 并且使用端口 42048.

我正在将 awsvpc 网络与 Fargate 一起使用,我必须这样做。

Angular 应用程序使用 localhost:42048/some_url 与后端通信,它在我的本地 docker 中工作正常,但在 AWS 中,前端不工作找到后端。目前我的端口映射为前端为 80,后端为 42048,在本地部署时前端能够找到后端 localhost:42048

如有任何帮助,我们将不胜感激。谢谢

link在 AWSVPC 中不允许访问。

当设置为网桥时,您只能在网络模式下进行 linking。

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. For more information about linking Docker containers, go to https://docs.docker.com/engine/userguide/networking/default_network/dockerlinks/. This parameter maps to Links in the Create a container section of the Docker Remote API and the --link option to docker run.

备注

使用 awsvpc 网络模式的 Windows 容器或任务不支持此参数。

重要

并置在单个容器实例上的容器可以相互通信,而无需 links 或主机端口映射。使用安全组和 VPC 设置在容器实例上实现网络隔离。

task_definition_parameters

在网络模式下,您必须在同一个任务定义中定义两个容器,然后在link中提及容器的名称。

然后在前端容器中提到后端容器的名称。

对于 Fargate,如果您想使用 localhost:42048 访问您的后端,那么您可以尝试在同一个任务定义中配置您的前端和后端。在部署任务时,同一任务定义中定义的所有容器将 运行 在同一底层主机中,我们可以使用 localhost 访问它。 请记住,Fargate 存储是短暂的,您的后端不应在容器中维护应用程序状态。

...
"containerDefinitions": [
    {
      "name": "frontend",
      "image": "my-repo/angularapp",
      "cpu": 256,
      "memory": 1024,
      "essential": true,
      "portMappings": [ {
          "containerPort": 8080,
          "hostPort": 8080
       }
     ]
    },
    {
      "name": "backend",
      "image": "my-repo/springboot",
      "cpu": 256,
      "memory": 1024,
      "essential": true,
      "portMappings": [ {
          "containerPort": 42048,
          "hostPort": 42048
       }
     ]
    }
  ]
...  

但恐怕这种方法不适合生产级。