简体   繁体   English

从Docker容器中检测AWS主机环境

[英]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容器缺少curlwget ,则可以在Bash中使用此技巧:

if ( exec 2>/dev/null ; echo > /dev/tcp/169.254.169.254/80 ) ; then
   echo "AWS"
fi

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.

相关问题 AWS上的Docker-环境变量未从主机继承到容器 - Docker on AWS - Environment Variables not inheriting from host to container AWS XRay SDK 无法读取 docker 容器内的环境变量 - AWS XRay SDK fails to read environment variables within a docker container 将文件从Docker容器写入到AWS上的主机实例 - writing file from docker container to host instance on AWS 如何从 docker 容器内的主机工作站获取 aws 凭据 - how to get aws credentials from host workstation, inside docker container 从 AWS 上的 docker 容器内提供 Jupyter Notebook 不起作用? - Serving Jupyter Notebook from within docker container on AWS not working? 从 Docker 容器中获取 AWS 实例元数据? - Fetching AWS instance metadata from within Docker container? 在AWS Multicontainer Docker环境中部署到特定容器 - Deploying to specific container in AWS multicontainer docker environment 在连接到主机Node app的AWS ELB上运行Docker容器 - Running a Docker Container on AWS ELB, connected to the host Node app 如何承担在Elasticbeanstalk环境中运行的Docker容器中的AWS角色? - How to assume AWS role in docker container run on Elasticbeanstalk environment? docker容器中的'aws configure'不会使用环境变量或配置文件 - 'aws configure' in docker container will not use environment variables or config files
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM