Inquisitor Shm
Inquisitor Shm

Reputation: 1591

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? 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.

Currently, I am thinking that the simple way is to set an environment variable when running in AWS.

Is there another way?

Upvotes: 3

Views: 388

Answers (3)

Ricardo Branco
Ricardo Branco

Reputation: 6079

If your Docker container lacks curl or wget, you may use this trick in Bash:

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

Upvotes: 2

helloV
helloV

Reputation: 52385

Crude way of checking if you are running on AWS. All instances that are running on AWS have access to an internal metadata server with IP: 169.254.169.254. If you can connect to it, then you are on AWS. Otherwise you are not.

$ curl -s --connect-timeout 2 169.254.169.254 > /dev/null
$ echo $?
0

On non AWS machine:

$ curl -s --connect-timeout 2 169.254.169.254 > /dev/null
$ echo $?
28

Upvotes: 1

Robert
Robert

Reputation: 36763

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

Upvotes: 1

Related Questions