11

We are migrating from ECS to Fargate. In ECS, we could set the hostname in the task definition like this:
"hostname": "%HOST_NAME%"

It fails to create with the error 'hostname is not supported on container when networkMode=awsvpc' Is there any way to set hostname ?

vishnu narayanan
  • 3,813
  • 2
  • 24
  • 28
Sunil Shakya
  • 8,097
  • 2
  • 17
  • 20
  • comand parameter solve my problem `echo "$(ip a | grep -A2 eth1 | grep inet | awk '{print $2}' | sed 's#/.*##g' ) $(hostname)" >> /etc/hosts `. still unable to set hostname – Sunil Shakya Feb 23 '18 at 15:15
  • In your command should I replace $(hostname) with the actual hostname I want? For example, could I sub that in with $(mySpecialHostname)? If not, where do I actually specify the hostname in that command? – Coherent Nov 06 '18 at 16:44
  • Possible dupe: https://stackoverflow.com/q/50215187/4298208 – aaronsteers Jun 07 '19 at 20:56

4 Answers4

6

Although documentation is clear that this is not supported, there is a workaround. You can create a bootstrap_ecs.sh file and override the container ENTRYPOINT to reference this at runtime (or else add the below to your own bootstrap script). You can use this when running from ECS. Otherwise, use your standard ENTRYPOINT and COMMAND.

bootstrap_ecs.sh

#!/bin/bash

ifconfig # prints full IP info
echo "Detecting 'eth1' interface..."
DETECTED_IP=$(ifconfig -a | grep -A2 eth1 | grep inet | awk '{print $2}' | sed 's#/.*##g' | grep "\.")
if [[ -z $DETECTED_IP ]]; then
    echo "Detecting 'eth0' interface ('eth1' not found)..."
    DETECTED_IP=$(ifconfig -a | grep -A2 eth0 | grep inet | awk '{print $2}' | sed 's#/.*##g' | grep "\." | head -1)
fi
DETECTED_HOSTNAME=$(hostname)
echo -e "\n\nDETECTED_IP=$DETECTED_IP\nDETECTED_HOSTNAME=$DETECTED_HOSTNAME\n\n"
# Note: newer OS versions us `ip` instead of `ifconfig`

# Echo for debugging. You can comment/delete the 1st and 3rd lines once everything is working.
echo -e "Current file contents:\n $(cat /etc/hosts)"
echo "$DETECTED_IP $DETECTED_HOSTNAME" >> /etc/hosts
echo -e "\n\n\nUpdated file contents:\n $(cat /etc/hosts)"

CMD="$@"
$CMD
aaronsteers
  • 2,277
  • 2
  • 21
  • 38
  • Suggest simpler trick to extract IP from `ifconfig`. Try this: `ifconfig -a |awk '/eth0/,/^$/ {if ($1=="inet") print $2}` – Dudi Boy Jun 07 '19 at 22:05
4

AWS documentation says The hostname parameter is not supported if using the awsvpc networkMode. Anything but awsvpc is not supported with FARGATE, therefore there is no way to set hostname in task definition with FARGATE launch type.

Pavel
  • 335
  • 5
  • 16
  • 1
    Fwiw, although currently the highest rated answer, this answer is not fully correct - see my answer below which shows how to initialize hostname. Admittedly, it requires a custom bootstrap script but it is certainly possible to leverage. – aaronsteers Oct 31 '19 at 06:44
1

Let me share a tip that I have applied for setting up the Avro Schema Registry. It could be helpful in answering this question. Schema Registry requires setting the SCHEMA_REGISTRY_HOST_NAME environment variable. With AWS Fargate and awsvpc networking mode there is no way to explicitly set the host name property inside the task definition, as was already pointed out in this thread. For horizontal scaling you need multiple registry instances each having its own SCHEMA_REGISTRY_HOST_NAME value. I had overridden the Command property in a task definition in the following manner (the HOSTNAME is provided by Docker and under awsvpc regime it reliably reflects the host name):

sh,-c,export SCHEMA_REGISTRY_HOST_NAME=$HOSTNAME;/etc/confluent/docker/run

The downside of this approach is that you now depend on the implementation detail of the image; in this case the original CMD statement from the Dockerfile. This is why it is always important to use a specific version tag with an image name instead of relying on the latest tag.

P.S. As a reference, here is my original comment on Issues 1126 for Schema Registry.

  • Thanks, Ervin! $HOSTNAME arg idea was useful for me. – Ruslan Danilin Dec 18 '20 at 04:58
  • 2
    This approach has stopped working. Adding this comment, hoping it will help future readers. Looks like AWS ECS is in the process of changing the value of HOSTNAME variable in ECS tasks/containers. I had almost identical implementation, which was working in my dev aws account. It failed in prod aws account as $HOSTNAME is no more the familiar "ip-a-b-c-d..compute.internal". Instead it is "- – Kash Sep 09 '21 at 16:36
1

I recently experienced this same problem however we took a different path than setting the hostname for each task.

There is an outstanding GitHub issue against the datadog-agent that applies when it is run in a sidecar container on ECS and Fargate: https://github.com/DataDog/datadog-agent/issues/3159

This lead to the discovery of the DD_DOGSTATSD_TAG_CARDINALITY=orchestrator setting which automatically adds a task_arn tag when running on ECS and Fargate. https://docs.datadoghq.com/getting_started/tagging/assigning_tags/?tab=containerizedenvironments#environment-variables

ic_
  • 31
  • 2