If I am running my docker based container in AWS ECS (EC2 Container Service), is there a way I can identify from inside the application whether my container is running on AWS ECS or not? This is required, because my docker container can run on any platform, but when it is running on AWS ECS, I need to perform some extra operations.
Maybe you can use the Amazon ECS Container Agent Introspection:
The Amazon ECS container agent provides an API for gathering details about the container instance that the agent is running on and the associated tasks that are running on that instance.
You can use the
curl
command from within the container instance to query the Amazon ECS container agent (port 51678) and return container instance metadata or task information.
For instance, from within your container:
[ec2-user ~]$ curl http://localhost:51678/v1/metadata
Output:
{
"Cluster": "default",
"ContainerInstanceArn": "<container_instance_ARN>",
"Version": "Amazon ECS Agent - v1.14.1 (467c3d7)"
}
Another criteria, as mention by the OP in the comments, is the Instance MetaData and User Data
Instance metadata is data about your instance that you can use to configure or manage the running instance. Instance metadata is divided into categories.
To view all categories of instance metadata from within a running instance, use the following URI:
http://169.254.169.254/latest/meta-data/
Note that you are not billed for HTTP requests used to retrieve instance metadata and user data.
You can use a tool such as cURL, or if your instance supports it, the GET command; for example:
[ec2-user ~]$ curl http://169.254.169.254/latest/meta-data/
So a successful curl
is enough:
curl -sL -w "%{http_code}\\n" "http://169.254.169.254/latest/meta-data/" -o /dev/null
That will display 200 if OK.
See "Linux script with curl to check webservice is up"
After a lot of trial and error, I found the following most helpful:
Invoke http://169.254.169.254/latest/meta-data/
If you get 200 OK, then you can assume you are running inside AWS EC2/ECS.
But if you don't get 200 OK, then you are not running in AWS EC2/ECS.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With