2

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

kozlone
  • 93
  • 1
  • 6

2 Answers2

2

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 find ECS_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 :)

MLu
  • 23,798
  • 5
  • 54
  • 81
  • Yes, you are right. The vars are in place. Regarding ssh-ing to containers. Yeah, I wasn’t aware of some nuances of a project when was choosing tools for deployment. We need to debug the app on the “server” (containers). So, maybe switching to EC2 hosted containers would be the right way, but for now I don’t want to break the current approach because it will block the development process – kozlone Jun 24 '20 at 05:18
  • BTW a lot of people are struggling about this: https://github.com/aws/containers-roadmap/issues/187 So, I think I’m not the only one implementing this anti-pattern. Also, I think I don’t really understand how it affects the system, i.e. why ssh-ing to a container considered as an anti-pattern? – kozlone Jun 24 '20 at 05:22
1

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.

kozlone
  • 93
  • 1
  • 6