[英]Detect AWS host environment from within a Docker Container
From within a Docker container, how can I detect that I am running inside an AWS environment? 如何从Docker容器中检测到我正在AWS环境中运行? I want the same container to optionally execute some AWS commands on startup IF running from within AWS, but skip those if running in a local environment. 我希望同一个容器有选择地在从AWS内部运行的启动IF上执行一些AWS命令,但是如果在本地环境中运行,则跳过这些命令。
Currently, I am thinking that the simple way is to set an environment variable when running in AWS. 当前,我认为简单的方法是在AWS中运行时设置环境变量。
Is there another way? 还有另一种方法吗?
If your Docker container lacks curl or wget , you may use this trick in Bash: 如果您的Docker容器缺少curl或wget ,则可以在Bash中使用此技巧:
if ( exec 2>/dev/null ; echo > /dev/tcp/169.254.169.254/80 ) ; then
echo "AWS"
fi
For instance: 例如:
curl -i http://169.254.169.254/ | grep "200 OK"
Docs: http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/identify_ec2_instances.html 文件: http : //docs.aws.amazon.com/AWSEC2/latest/UserGuide/identify_ec2_instances.html
Crude way of checking if you are running on AWS. 检查您是否在AWS上运行的粗略方法。 All instances that are running on AWS have access to an internal metadata server with IP: 169.254.169.254
. 在AWS上运行的所有实例都可以访问IP为169.254.169.254
的内部元数据服务器。 If you can connect to it, then you are on AWS. 如果您可以连接到它,那么您就在AWS上。 Otherwise you are not. 否则你不是。
$ curl -s --connect-timeout 2 169.254.169.254 > /dev/null
$ echo $?
0
On non AWS machine: 在非AWS机器上:
$ curl -s --connect-timeout 2 169.254.169.254 > /dev/null
$ echo $?
28
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.