提问者:小点点

将环境变量传递给Docker容器


目前使用弹性豆茎来运行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"
}

但是这不起作用。当SSHing到我的beanstek实例然后获取环境变量的内容时,我可以看到它们没有初始化:

[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个答案

匿名用户

单个容器的Dockerrun.aws. json中不允许使用“环境”字段。

但是,您可以使用以下过程在. config文件中指定环境变量(查看留档以获取更多信息):

  1. 创建一个文件夹. eb扩展
  2. 在文件夹中创建一个. config文件
  3. 填写配置文件:
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

匿名用户

另一种方式(图形):

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