I am running a third party application in AWS ECS, with fargate as the launch type. This application requires the host IP to be passed as an environment variable. But in fargate launch type, I would not know the IP until the tasks are up and running. Therefore I am planning to modify the DockerFile to have host IP added as an environment variable. How can I do this?
Asked
Active
Viewed 287 times
1 Answers
1
You want to use task metadata to introspect your task and container characteristics (which includes its private IP). You essential need to curl ${ECS_CONTAINER_METADATA_URI_V4}/task
from within the container and parse the response to find what you need. Refer to this documentation for the structure of the JSON returned.

mreferre
- 5,464
- 3
- 22
- 29
-
Thanks, but 1. I need to set the variable during the creation of the container 2. I cannot 'get inside' the container once it is up and running because of the Fargate launch type. Then how can I curl it? – M S Puranik Apr 27 '21 at 11:13
-
What I was suggesting is that you populate the variable at run time in a startup script not at configuration time (also because, as you said, you don't know and can't know the IP upfront). I did not mean to populate the variable manually (albeit you could do that with the new feature dubbed [ECS Exec](https://aws.amazon.com/blogs/containers/new-using-amazon-ecs-exec-access-your-containers-fargate-ec2/) we launched recently). I meant to run the curl as part of your startup script, save the IP in a variable and then launch your app from the script. – mreferre Apr 27 '21 at 11:19
-
I think there is a confusion here. I want to set the HOST's IP (the machine ON which the container is running) as an environment variable IN the container. The task metadata provides the IP of the container itself, not of the underlying host. – M S Puranik May 07 '21 at 07:39
-
1Yes I think there is confusion because there is no host at all if you use Fargate (or not that you can access/deal with anyway). Definitely there is no "IP address for the host". That is the whole idea of Fargate: deploy containers without needing to have/manage/see infrastructure. What is the reason why you need the host IP? Why do the app need that? – mreferre May 07 '21 at 12:34
-
Basically the container on which I am trying to set the env variable is is a 'load generator'. This container is going to send requests to a target for the purpose of load testing. There exists another container which is a 'controller'. A controller manages one ore more LGs. So it should be able to contact any LG over $LG_HOST:PORT. This is a third party tool, so I do not have much freedom to modify the logic. Controller: https://hub.docker.com/r/neotys/neoload-controller LG: https://hub.docker.com/r/neotys/neoload-loadgenerator/ – M S Puranik May 08 '21 at 15:18
-
I suspect that the `$LG_HOST:PORT` comes from the assumption of a regular docker setup in bridge mode where the IP that you need to use to reach your container is the IP of the host (and then the port you used to expose your container). With Fargate and in general the `awsvpc` networking mode the IP of the task IS the IP you can use to access your container and the port is just the port of your application. Given each task gets a dedicated VPC ip address there is not need to map a port on the host with the application port. TL/DR: you can still use the mechanism I was suggesting. – mreferre May 08 '21 at 17:41