将环境变量传递给Docker容器

时间:2015-07-01 15:58:35

标签: amazon-web-services docker elastic-beanstalk

目前使用Elastic Beanstalk运行Docker容器,我需要将重要信息作为环境变量传递给我的容器。

我现在的Dockerrun.aws.json看起来像这样:

{
    "AWSEBDockerrunVersion": "1",
    "Image": {
        "Name": "b2boost/rabbitelasticdockstash",
        "Update": "true"
    },
    "Ports": [
        {
            "ContainerPort": "80"
        }
    ],
    "environment": [
        {
            "name": "RABBITMQ_HOST",
            "value": "RABBITMQ_HOST"
        },
        {
            "name": "RABBITMQ_PASSWORD",
            "value": "RABBITMQ_PASSWORD"
        },
        {
            "name": "RABBITMQ_USER",
            "value": "RABBITMQ_USER"
        },
        {
            "name": "RABBITMQ_VHOST",
            "value": "RABBITMQ_VHOST"
        },
        {
            "name": "ELASTICSEARCH_HOST",
            "value": "ELASTICSEARCH_HOST"
        },
        {
            "name": "ELASTICSEARCH_PASSWORD",
            "value": "ELASTICSEARCH_PASSWORD"
        },
        {
            "name": "ELASTICSEARCH_PORT",
            "value": "ELASTICSEARCH_PORT"
        },
        {
            "name": "ELASTICSEARCH_PROTOCOL",
            "value": "ELASTICSEARCH_PROTOCOL"
        },
        {
            "name": "ELASTICSEARCH_USER",
            "value": "ELASTICSEARCH_USER"
        }
    ],
    "Volumes": [
    ],
    "Logging": "/var/log/eb-activity.log"
}

然而,这不起作用。在SSH访问我的beanstalk实例然后获取环境变量的内容时,我可以看到它们没有被初始化:

[ec2-user@myip ~]$ sudo docker exec goofy_curie env

PATH=/opt/logstash/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:bin
HOSTNAME=HOSTNAME
LANG=C.UTF-8
JAVA_VERSION=7u79
JAVA_DEBIAN_VERSION=7u79-2.5.5-1~deb8u1
LOGSTASH_MAJOR=1.5
LOGSTASH_VERSION=1:1.5.1-1
HOME=/root

如何在容器中设置环境变量? Dockerrun.aws.json似乎不适合我。

2 个答案:

答案 0 :(得分:14)

"环境"对于单个容器,Dockerrun.aws.json中不允许使用该字段。

但是,您可以使用以下过程在.config文件中指定环境变量(有关详细信息,请查看documentation):

  1. 创建文件夹.ebextensions
  2. 在文件夹
  3. 中创建.config文件
  4. 填写配置文件:
  5. option_settings:  
      - option_name: RABBITMQ_HOST
        value: RABBITMQ_HOST
      - option_name: RABBITMQ_PASSWORD
        value: RABBITMQ_PASSWORD
      - option_name: RABBITMQ_USER
        value: RABBITMQ_USER
      - option_name: RABBITMQ_VHOST
        value: RABBITMQ_VHOST
      - option_name: ELASTICSEARCH_HOST
        value: ELASTICSEARCH_HOST
      - option_name: ELASTICSEARCH_PASSWORD
        value: ELASTICSEARCH_PASSWORD
      - option_name: ELASTICSEARCH_PORT
        value: ELASTICSEARCH_PORT
      - option_name: ELASTICSEARCH_PROTOCOL
        value: ELASTICSEARCH_PROTOCOL
      - option_name: ELASTICSEARCH_USER
        value: ELASTICSEARCH_USER
    
    1. 将.ebextensions文件与Dockerrun.aws.json一起压缩并上传到Beanstalk

答案 1 :(得分:2)

另一种方式(图形):

在您的beanstalk配置中,单击软件卡的修改。在此页面的底部,您可以填写环境变量的名称和值。