ECS:环境变量在任务中设置但在容器

时间:2017-05-24 18:02:18

标签: amazon-web-services docker amazon-ecs

我有一个如下所示的任务定义:

> aws ecs describe-task-definition --task-definition ruby-on-rails-test
{
    "taskDefinition": {
        "status": "ACTIVE",
        "networkMode": "bridge",
        "family": "ruby-on-rails-test",
        "placementConstraints": [],
        "requiresAttributes": [
            {
                "name": "com.amazonaws.ecs.capability.ecr-auth"
            },
            {
                "name": "com.amazonaws.ecs.capability.docker-remote-api.1.21"
            }
        ],
        "volumes": [
            {
                "host": {
                    "sourcePath": "/opt/cf/rails-app/public/"
                },
                "name": "ruby-on-rails-public-volume"
            }
        ],
        "taskDefinitionArn": "arn:aws:ecs:us-east-1:accountId:task-definition/ruby-on-rails-test:5",
        "containerDefinitions": [
            {
                "memoryReservation": 1024,
                "environment": [
                    {
                        "name": "DATABASE_HOSTNAME",
                        "value": "hostname"
                    },
                    {
                        "name": "PUMA_WORKERS",
                        "value": "2"
                    },
                    {
                        "name": "RAILS_ENV",
                        "value": "staging"
                    },
                    {
                        "name": "DATABASE_NAME",
                        "value": "ruby-on-rails"
                    },
                    {
                        "name": "DEBIAN_FRONTEND",
                        "value": "noninteractive"
                    },
                    {
                        "name": "PORT",
                        "value": "8080"
                    },
                    {
                        "name": "LANG",
                        "value": "en_US.UTF-8"
                    },
                    {
                        "name": "DATABASE_PASSWORD",
                        "value": "cf"
                    },
                    {
                        "name": "DATABASE_USER",
                        "value": "cf"
                    },
                    {
                        "name": "PUMA_MAX_THREADS",
                        "value": "6"
                    }
                ],
                "name": "ruby-on-rails-test",
                "mountPoints": [
                    {
                        "sourceVolume": "ruby-on-rails-public-volume",
                        "containerPath": "/opt/cf/rails-app/public/"
                    }
                ],
                "image": "accountId.dkr.ecr.us-east-1.amazonaws.com/cf/rails:latest",
                "cpu": 1024,
                "portMappings": [
                    {
                        "protocol": "tcp",
                        "containerPort": 8080,
                        "hostPort": 8080
                    }
                ],
                "command": [
                    "puma",
                    "-C",
                    "config/puma.rb"
                ],
                "essential": true,
                "volumesFrom": []
            }
        ],
        "revision": 5
    }
}

当容器引导时,不会设置环境变量。我从ecs容器机器中运行了以下命令,从一个无法启动的容器

[root@ip-ip docker]# docker run --name 46485594ffc4 accountId.dkr.ecr.us-east-1.amazonaws.com/    cf/rails:latest env
PATH=/usr/local/bundle/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=0e0656e4e84c
RUBY_MAJOR=2.3
RUBY_VERSION=2.3.4
RUBY_DOWNLOAD_SHA256=341cd9032e9fd17c452ed8562a8d43f7e45bfe05e411d0d7d627751dd82c578c
RUBYGEMS_VERSION=2.6.12
BUNDLER_VERSION=1.15.0
GEM_HOME=/usr/local/bundle
BUNDLE_PATH=/usr/local/bundle
BUNDLE_BIN=/usr/local/bundle/bin
BUNDLE_SILENCE_ROOT_WARNING=1
BUNDLE_APP_CONFIG=/usr/local/bundle
APP_HOME=/opt/cf/rails-app
HOME=/root

知道为什么没有设置变量?

提前致谢。

1 个答案:

答案 0 :(得分:3)

当您自己docker run图像时,您错过了AWS为运行容器所做的全部事情。它将env变量设置在docker run

你这样做:

  

docker run --name 46485594ffc4 accountId.dkr.ecr.us-east-1.amazonaws.com/cf/rails:latest env

在那里,您对配置的任务一无所知。你只有你推动的图像。

因此,要检查已停止容器中的环境变量,您应该首先找到它:

docker ps -a

然后使用容器ID,执行:

docker inspect ID

你应该看到env vars。

PS:要查看该容器会发生什么,请检查其日志:docker logs ID