gpt4 book ai didi

amazon-web-services - 如何确定我的容器是否在 AWS ECS 上运行?

转载 作者:行者123 更新时间:2023-12-05 00:15:37 27 4
gpt4 key购买 nike

如果我在 AWS ECS(EC2 容器服务)中运行基于 docker 的容器,有没有办法从应用程序内部识别我的容器是否在 AWS ECS 上运行?这是必需的,因为我的 docker 容器可以在任何平台上运行,但是当它运行在 AWS ECS 上时,我需要执行一些额外的操作。

最佳答案

也许你可以使用 Amazon ECS Container Agent Introspection :

The Amazon ECS container agent provides an API for gathering details about the container instance that the agent is running on and the associated tasks that are running on that instance.

You can use the curl command from within the container instance to query the Amazon ECS container agent (port 51678) and return container instance metadata or task information.



例如,从您的容器中:
[ec2-user ~]$ curl http://localhost:51678/v1/metadata

输出:
{
"Cluster": "default",
"ContainerInstanceArn": "<container_instance_ARN>",
"Version": "Amazon ECS Agent - v1.14.1 (467c3d7)"
}

OP in the comments 提到的另一个标准,是 Instance MetaData and User Data

Instance metadata is data about your instance that you can use to configure or manage the running instance. Instance metadata is divided into categories.

To view all categories of instance metadata from within a running instance, use the following URI:


http://169.254.169.254/latest/meta-data/

Note that you are not billed for HTTP requests used to retrieve instance metadata and user data.

You can use a tool such as cURL, or if your instance supports it, the GET command; for example:


[ec2-user ~]$ curl http://169.254.169.254/latest/meta-data/

这么成功 curl足够:
 curl -sL -w "%{http_code}\\n" "http://169.254.169.254/latest/meta-data/" -o /dev/null

如果正常,将显示 200。
见“ Linux script with curl to check webservice is up

关于amazon-web-services - 如何确定我的容器是否在 AWS ECS 上运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44193262/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com