The question is pretty straightforward but a little background always could be helpful.
I need to get a container id from the container itself, and the value of it will be used as a logfile name.
According to the docs ECS_CONTAINER_METADATA_URI_V4
is injected to each container.
But when I'm connection to a Fargate hosted containers through SSH, there is no such variable. Which is expected and I had created the topic for that before just to make sure my guess is correct.
So, is there a way to get the value of ECS_CONTAINER_METADATA_URI_V4
I’m pretty sure that the application running in the container will have that environment variable available.
The problem is with ssh and bash that clear / sanitise the environment vars before you get the shell. I suggest you run the task on EC2-based ECS for testing (ie not on Fargate) and
docker exec -it {container} bash
into it - more likely than not you will findECS_CONTAINER_METADATA_URI_V4
in the env.BTW - ssh’ing into a container is a bad bad practice. Containers are immutable, there should be no interactive access.
Hope that helps :)
For now, I came with persisting the value of
ECS_CONTAINER_METADATA_URI_V4
by saving it to file on a container startup:echo ${ECS_CONTAINER_METADATA_URI_V4} > metadata_url.txt
So then I can access it.