如何从ECS容器中获取任务ID? [英] How to get Task ID from within ECS container?

查看:147
本文介绍了如何从ECS容器中获取任务ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我有兴趣从EC2主机内部运行容器中检索Task ID.

Hello I am interested in retrieving the Task ID from within inside a running container which lives inside of a EC2 host machine.

AWS ECS文档指出存在环境变量ECS_CONTAINER_METADATA_FILE以及此数据的位置,但是只有在创建集群/EC2实例时将ECS_ENABLE_CONTAINER_METADATA变量设置为true时,才能设置/可用.我看不到在aws控制台中可以在哪里完成.

AWS ECS documentation states there is an environment variable ECS_CONTAINER_METADATA_FILE with the location of this data but will only be set/available if ECS_ENABLE_CONTAINER_METADATA variable is set to true upon cluster/EC2 instance creation. I don't see where this can be done in the aws console.

此外,文档指出,可以通过在主机内部将其设置为true来完成此操作,但需要重新启动Docker代理.

Also, the docs state that this can be done by setting this to true inside the host machine but would require to restart the docker agent.

还有其他方法可以执行此操作,而不必进入EC2内部进行设置并重新启动Docker代理吗?

Is there any other way to do this without having to go inside the EC2 to set this and restart the docker agent?

推荐答案

我要使用的技术是在容器定义中设置环境变量.

The technique I'd use is to set the environment variable in the container definition.

如果您正在管理任务,请通过通过Cloudformation ,相关的Yaml如下所示:

If you're managing your tasks via Cloudformation, the relevant yaml looks like so:

  Taskdef:
    Type: AWS::ECS::TaskDefinition
    Properties:
      ...
      ContainerDefinitions:
        - Name: some-name
          ...
          Environment:
            - Name: AWS_DEFAULT_REGION
              Value: !Ref AWS::Region
            - Name: ECS_ENABLE_CONTAINER_METADATA
              Value: 'true'

此技术可帮助您使所有内容保持直接性和可重复性.

This technique helps you keep everything straightforward and reproducible.

如果您以编程方式需要元数据并且无权访问元数据文件,则可以查询

If you need metadata programmatically and don't have access to the metadata file, you can query the agent's metadata endpoint:

curl http://localhost:51678/v1/metadata

请注意,如果您正在作为运行任务获取此信息,则可能无法连接到回送设备,但可以连接到EC2实例自己的IP地址.

Note that if you're getting this information as a running task, you may not be able to connect to the loopback device, but you can connect to the EC2 instance's own IP address.

这篇关于如何从ECS容器中获取任务ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆