Sabbir Ahmed
Sabbir Ahmed

Reputation: 75

How to put ECS Docker Container ID into a Environmental Variable inside the Container

Hi I dont know if this is possible but I have a docker container in aws

[ec2-user@ip-10-200-40-182 ~]$ docker ps
CONTAINER ID        IMAGE                                                                                                                                                                            
74566049de08        xxxxxx.xxxxx.io/customers/xxxxxxx/lin/ema:5.0.6

I want to take that container ID and put it in a environmental variable inside that docker container

This needs to be dynamic so everytime i bounce the ecs service the environmental variable should automatically change.

I tried to do this in the dockerfile

FROM xxxxxxxxxxx.xxxx.ecr.eu-west-1.amazonaws.com/xxxxxxxxx:xxxxxxx

RUN uname -n > /test/tmp.log && \
export test_JMX_MANAGEMENTNODEHOSTNAME=$(cat /test/tmp.log)

But everytime I got into the container and echo $test_JMX_MANAGEMENTNODEHOSTNAME I get $(cat /test/tmp.log) and not the container id.. I am abit new to docker so help would be appreciated thank you!

Upvotes: 0

Views: 1061

Answers (2)

Sabbir Ahmed
Sabbir Ahmed

Reputation: 75

I managed to figure it out what I exactly needed to do, I just ran it through entrypoint in the dockerfile and it worked :)

Also @Durja thanks for your info! Helped me figure out my next problem related to IP ☺️

Upvotes: 0

Durja
Durja

Reputation: 667

If you can install aws cli within the container, you can call the ecs describe task when the container starts to get the container id and set it as env variable within your container.

Reference:

https://docs.aws.amazon.com/AmazonECS/latest/APIReference/API_Container.html

https://docs.aws.amazon.com/cli/latest/reference/ecs/describe-tasks.html

Upvotes: 1

Related Questions