[ECS] Need Host environment variable resolution to pass some information to a container
Before moving to ECS, I need to be able to make sure that I can run consul in that environment. To run consul, I need to pass some Host environment variables as environment variables to the container. I would normally do this as -e HOST=$HOST etc. However, it seems that that if I define a variable with $HOST in a task definition, this is treated as a static string.
Related, I also typically would set docker command line options on the run command for several features I need. I may be able to get around some of this by overriding options in the docker daemon itself, but as far as passing host information I am not aware of any workaround.
I'd appreciate a few clarifications around what you need here.
When you mention passing in host environment variables, what are you thinking about passing in and how are they getting set on the host? Do you just need e.g. the hostname or ip, or do you want to access arbitrary host environment variables?
Also, if you could pass in environment variables as container overrides when you call start-task, would that help?
As it is, you can access the EC2 Metadata Service from within your containers to get the IP, Instance ID, and other information.
I'd like to understand the use-case a little better and any other possible solutions.
Best, Euan
Thank you for the quick reply!
My end goal is to get several docker "side cars" working with ECS (service discovery, log shipping, etc). Some of these containers expect some host information to be passed in at run time, like the host ip, hostname, etc. I think currently most or all of the items I need are host specific. Currently all our deployments are automated via CloudFormation and the environment variables are set during cloud-init which also launches the containers. You raise a good point about the ec2 metadata service. I could probably fork some of these projects to use it, the only drawback being the containers are more tied to AWS so harder to run offline, etc.
The other option I was just considering is to load these containers which require special config on each of the ECS hosts I create via Cloud Formation via current process. ECS would then only manage my other containers. I am wondering though if that would muck with the CPU and Memory allocation available to tasks?
I have a related question in that I have many containers that need to pass docker run arguments such as --dns -h $HOST --restart=always etc. I think some of these I can set in the docker daemon startup options in /etc/syconfig/ but some can't be set that way. Are you guys thinking about how docker run options like these might be used or specified?
Thanks,
Andrew
Sorry for the slow response on this; passing through host environment variables is a tricky subject.
First of all, you have a few other questions that are easier to address.
- CPU / Memory allocation available:
Yes, running things on your instance that ECS is unaware of would cause issues there, but we want to support that use-case. Being able to override the resources ECS can use at launch makes a lot of sense to me. Good question.
- Docker run arguments
You're correct that anything that can be passed to the docker daemon (such as the ones you reference) can be edited by using the following user-data:
#cloud-boothook
#!/bin/sh
echo 'OPTIONS="--dns=8.8.8.8"' > /etc/sysconfig/docker
If there are any specific docker run features you want that are not covered by our task definition, we'd be interested in hearing about them.
- Host environment variables
To clarify a little further, when you say your environment variables are "set with cloud init" you mean you've put the desired data in /etc/environment or similar, correct?
Would being able to just specify the equivilant of docker's "--env-file" as part of a container definition fit your use case, or would that be insufficient for some reason?
Hello,
I re-lauch the thread as I'm interested too, and I believe my use-case is close to the one described here.
In the task definition, I can statically set anything related to the environment of the running task (domain, ...), but I also need to run my docker container with arguments depending on the host on which it is launched (host IP address, AWS instance Id, ...).
My docker run command would be something like:
docker run -d -p=80:80 -e HOST=$HOST myrepo/myimage java -Dmy.local.ip=$(curl 169.254.169.254/latest/meta-data/local-ipv4 2> /dev/null) -jar /path/to/my/jarfile.jar
And would correspond to:
docker run -d -p=80:80 -e HOST=myhostname myrepo/myimage java -Dmy.local.ip=10.0.0.5 -jar /path/to/my/jarfile.jar
Is there a way to reproduce this kind of behaviour with a task definition ?
Thanks,
Arthur
@radenui Currently there's not a direct way to reproduce that with a task definition.
However, if you change your image to have an entrypoint file where you resolve the above, you can get similar behavior. For example, such an entrypoint might look like:
#!/bin/sh
export HOST=$(curl -s 169.254.169.254/latest/meta-data/local-hostname)
export LOCAL_IP=$(curl -s 169.254.169.254/latest/meta-data/local-ipv4)
exec "$@"
With the above entrypoint, you can reference those environment variables in your command.
I'm still leaving this issue open since I think the above is probably not the right long-term solution (especially for common things like instance ip).
Best, Euan
I had to table this temporarily but the end goal is I need consul and registrator working in ECS if I'm going to migrate our services. The consul / registrator docker containers require the $HOST and $HOSTNAME to be passed in to correctly register services and advertise the host machine IP. I was trying to avoid forking the Docker container with a solution like the entrypoint as that ties the container to AWS.
The other original blocker is on my own containers I need to set the DNS to the docker bridge via the --dns flag. I think #2 might work for that if I run the ECS agent on my own tailored machined with that setting.
+1
would love to see this as well, along with most of the docker-run flags, necessary for logging & metrics unless you want to run those via ansible/terraform etc
+1
having HOST and LOCAL_IP directly defined would be much appreciated. My other option would have to link directly to the container that I wan't to access, but it's not actually possible to link to a container not defined in a Task (https://forums.aws.amazon.com/thread.jspa?threadID=179653&tstart=0)
I'm also interested in this to pass database connection information
+1
Also interested in this feature as it fills in some gaps for auto-registration of containers in our environment as well.
+1 :+1:
I would like to have this feature as well.
+1, same use case as @owaaa - I want to use consul + registrator, so I need a clean way to pass host-specific config into a container.
+1 Being able to pass in the host or other dynamic values specific to the host is valuable either through a environment variable or possibly through a CMD statement.
Specifically when using third party containers where you don't want to create your own version of.
+1; My use case is also described above
@euank I have been trying your thought on the entrypoint and can't get your bash script to work. Do you have a working example?
@ccit-spence I put together a short example that worked for me which I've posted in this gist.
+1, consul & registrator use case as well!
+1. Need support for docker-run flags.ECS not supporting Docker defaults is puzzling (at best).
Why has this not been implemented yet? On March 3 the proposed workaround to ECS shortcomings was identified as "probably not the right long-term solution (especially for common things like instance ip)". Five months have passed...
+1