How to configure hostname in ECS Fargate task definition

前端 未结 4 1784
攒了一身酷
攒了一身酷 2021-02-08 21:42

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 e

4条回答
  •  北恋
    北恋 (楼主)
    2021-02-08 22:17

    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
    

提交回复
热议问题