AWS ECS环境变量未设置

时间:2017-06-02 14:46:31

标签: amazon-web-services amazon-ecs

由于某种原因,环境变量(虽然我已在ECS任务中配置它们)未在运行容器中设置。我错过了什么?为什么值为空?

我有以下AWS::ECS::TaskDefinition

AirflowWebTaskDefinition:
  Type: AWS::ECS::TaskDefinition
  Properties:
    Family: !Join ['', [!Ref 'AWS::StackName', -dl-airflow-web]]
    ContainerDefinitions:
    - Name: dl-airflow-web
      Cpu: '10'
      Essential: 'true'
      Image: companyname-docker-snapshot-local.jfrog.io/docker-airflow:1.0
      Command: ['webserver']
      Memory: '1024'
      LogConfiguration:
        LogDriver: awslogs
        Options:
          awslogs-group: !Ref 'AirflowCloudwatchLogsGroup'
          awslogs-region: !Ref 'AWS::Region'
          awslogs-stream-prefix: dl-airflow-web
      PortMappings:
      -
        ContainerPort: 8080
      Environment:
      - Name: LOAD_EX
        Value: n
      - Name: EXECUTOR
        Value: Celery
      - Name: MYQL_HOST
        Value: !Ref 'RDSDNSName'
      - Name: MYSQL_PORT
        Value: !Ref 'RDSPort'
      - Name: MYSQL_DB
        Value: !Ref 'AirflowDBName'
      - Name: USERNAME
        Value: !Ref 'AirflowDBUser'
      - Name: PASSWORD
        Value: !Ref 'AirflowDBPassword'

我正在使用一个码头图像,它是https://github.com/puckel/docker-airflow的一个分支。图像的entrypoint检查环境变量,如下所示:

#!/usr/bin/env bash

AIRFLOW_HOME="/usr/local/airflow"
CMD="airflow"
TRY_LOOP="20"

: ${MYSQL_HOST:="default-mysql"}
: ${MYSQL_PORT:="3306"}

如果$MYSQL_*变量未在docker run命令中设置,则将其设置为默认值。

当我使用下面的配置从docker-compose运行容器图像时,它可以工作并且环境变量都已设置:

webserver:
  image: companyname-docker-snapshot-local.jfrog.io/docker-airflow:1.0
  environment:
    - LOAD_EX=n
    - EXECUTOR=Celery
    - MYSQL_HOST=mysql
    - MYSQL_PORT=3306
    - USERNAME=dev-user
    - PASSWORD=dev-secret-pw
    - SQS_HOST=sqs
    - SQS_PORT=9324
    - AWS_DYNAMODB_ENDPOINT=http://dynamodb:8000
  ports:
    - "8090:8080"
  command: webserver

我的entrypoint.sh中的以下命令:

  echo "$(date) - Checking for MYSQL (host: $MYSQL_HOST, port: $MYSQL_PORT) connectivity"

记录此输出:

Fri Jun  2 12:55:26 UTC 2017 - Checking for MYSQL (host: mysql, port: 3306) connectivity

但是检查我的cloudwatch日志会显示此输出的默认值:

Fri Jun 2 14:15:03 UTC 2017 - Checking for MYSQL (host: default-mysql, port: 3306) connectivity

但我可以ssh进入EC2主机,运行docker inspect [container_id]并验证是否已设置环境变量:

Config": {
    "Hostname": "...",
    "Domainname": "",
    "User": "airflow",
    "AttachStdin": false,
    "AttachStdout": false,
    "AttachStderr": false,
    "ExposedPorts": {
        "5555/tcp": {},
        "8080/tcp": {},
        "8793/tcp": {}
    },
    "Tty": false,
    "OpenStdin": false,
    "StdinOnce": false,
    "Env": [
        "MYSQL_PORT=3306",
        "PASSWORD=rds-secret-pw",
        "USERNAME=rds-user",
        "EXECUTOR=Celery",
        "LOAD_EX=n",
        "MYQL_HOST=rds-cluster-name.cluster-id.aws-region.rds.amazonaws.com",
        "MYSQL_DB=db-name",
        "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
        "DEBIAN_FRONTEND=noninteractive",
        "TERM=linux",
        "LANGUAGE=en_US.UTF-8",
        "LANG=en_US.UTF-8",
        "LC_ALL=en_US.UTF-8",
        "LC_CTYPE=en_US.UTF-8",
        "LC_MESSAGES=en_US.UTF-8"
    ],
    "Cmd": [
        "webserver"
    ],
    "Image": "companyname-docker-snapshot-local.jfrog.io/docker-airflow:1.0",
    "Volumes": null,
    "WorkingDir": "/usr/local/airflow",
    "Entrypoint": [
        "/entrypoint.sh"
    ],
    "OnBuild": null,
    "Labels": {
        "com.amazonaws.ecs.cluster": "...",
        "com.amazonaws.ecs.container-name": "...",
        "com.amazonaws.ecs.task-arn": "...",
        "com.amazonaws.ecs.task-definition-family": "...",
        "com.amazonaws.ecs.task-definition-version": "16"
    }
},

如果我跑:

$ docker exec [container-id] echo $MYSQL_HOST

输出为空

1 个答案:

答案 0 :(得分:0)

您的任务定义定义了env变量MYQL_HOST。你在docker compose中做到了这一点。只是CF,修复它,它应该没问题。