gpt4 book ai didi

c# - 是否可以从Docker容器内部访问任务定义

转载 作者:行者123 更新时间:2023-12-02 21:06:11 26 4
gpt4 key购买 nike

我希望能够从容器内部访问ECS的task.json中烘焙的某些值。可能吗?我知道我们可以在任务定义中添加environment部分,并且可以在docker容器中引用它,但是我也可以访问其他实体,例如,假设我想从容器内部访问awslogs-group。怎么做?

{
"family": "task-poc",
"containerDefinitions": [
{
"image": "ABC 123",
"name": "logging-poc-1",
"cpu": 1024,
"memory": 1024,
"essential": true,
"logConfiguration": {
"logDriver": "awslogs",
"options": {
"awslogs-group": "my_log_group",
"awslogs-region": "us-east-1",
"awslogs-stream-prefix": "foo-transactions-stg-secops"
}
}
}
]
}

对于环境变量,我可以像

在C#中- Environment.GetEnvironmentVariable("MyKey");

最佳答案

不,您不能像获取环境变量那样简单地获取日志组,但是可以在下面获取这些指标。

curl $ECS_CONTAINER_METADATA_URI

您可以从应用程序向 get container meta data发出此调用

要么

对于Linux实例:
cat $ECS_CONTAINER_METADATA_FILE

对于Windows实例(PowerShell):
Get-Content -path $env:ECS_CONTAINER_METADATA_FILE

这将返回
The following example shows a container metadata file in the READY status.

{
"Cluster": "default",
"ContainerInstanceARN": "arn:aws:ecs:us-west-2:012345678910:container-instance/1f73d099-b914-411c-a9ff-81633b7741dd",
"TaskARN": "arn:aws:ecs:us-west-2:012345678910:task/2b88376d-aba3-4950-9ddf-bcb0f388a40c",
"ContainerID": "98e44444008169587b826b4cd76c6732e5899747e753af1e19a35db64f9e9c32",
"ContainerName": "metadata",
"DockerContainerName": "/ecs-metadata-7-metadata-f0edfbd6d09fdef20800",
"ImageID": "sha256:c24f66af34b4d76558f7743109e2476b6325fcf6cc167c6e1e07cd121a22b341",
"ImageName": "httpd:2.4",
"PortMappings": [
{
"ContainerPort": 80,
"HostPort": 80,
"BindIp": "",
"Protocol": "tcp"
}
],
"Networks": [
{
"NetworkMode": "bridge",
"IPv4Addresses": [
"172.17.0.2"
]
}
],
"MetadataFileStatus": "READY"
}

最短方法:

将日志配置的值全部传递给ENV。
        "options": {
"awslogs-group": "my_log_group",
"awslogs-region": "us-east-1",
"awslogs-stream-prefix": "foo-transactions-stg-secops"
}

将以上内容传递给ENV
          "environment": [
{
"name": "awslogs-group",
"value": "my_log_group"
},
{
"name": "awslogs-region",
"value": "us-east-1"
},
{
"name": "awslogs-stream-prefix",
"value": "foo-transactions-stg-secops"
}
],

然后在C#中获得ENV
n C# - Environment.GetEnvironmentVariable("awslogs-group");

很长的路:
  • 从容器元数据d
  • 获取 TaskARN
  • 在应用程序中使用TaskArn描述任务
  • 从describe Task获得任务定义ARN
  • 描述任务定义,您将获得日志组
  • 关于c# - 是否可以从Docker容器内部访问任务定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57893404/

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