How do I get my IP address from inside an ECS container running with the awsvpc network mode?

时间:2019-04-17 00:37:35

标签: docker amazon-ecs aws-vpc

From a regular ECS container running with the bridge mode, or from a standard EC2 instance, I usually run

curl http://169.254.169.254/latest/meta-data/local-ipv4

to retrieve my IP.

In an ECS container running with the awsvpc network mode, I get the IP of the underlying EC2 instance which is not what I want. I want the address of the ENI attached to my container. How do I do that?

2 个答案:

答案 0 :(得分:2)

A new convenience environment variable is injected by the AWS container agent into every container in AWS ECS: ${ECS_CONTAINER_METADATA_URI}

This contains the URL to the metadata endpoint, so now you can do

curl ${ECS_CONTAINER_METADATA_URI}

The output looks something like

{  
   "DockerId":"redact",
   "Name":"redact",
   "DockerName":"ecs-redact",
   "Image":"redact",
   "ImageID":"redact",
   "Labels":{  },
   "DesiredStatus":"RUNNING",
   "KnownStatus":"RUNNING",
   "Limits":{  },
   "CreatedAt":"2019-04-16T22:39:57.040286277Z",
   "StartedAt":"2019-04-16T22:39:57.29386087Z",
   "Type":"NORMAL",
   "Networks":[  
      {  
         "NetworkMode":"awsvpc",
         "IPv4Addresses":[  
            "172.30.1.115"
         ]
      }
   ]
}

Under the key Networks you'll find IPv4Address

答案 1 :(得分:0)

从Amazon ECS控制台:

  1. 单击<您的集群>
  2. 点击<您的服务>
  3. 点击“任务”标签
  4. 单击<您的任务>
  5. 在此处查看网络详细信息

从CLI:

  • 如果是EC2,请登录到实例并运行docker exec -it hostname -f
  • 或只问ecs-ecs-cli ps --cluster < your cluster >

作为EC2模式下的VPC连接资源,它也在以下列表中列出:

  • EC2群集实例IP(即,检查EC2实例详细信息)
  • EC2中的“网络接口”部分(搜索对“ arn:aws:ecs”的描述)