16

Currently using Elastic Beanstalk to run Docker containers, I need to pass important information as environment variables to my containers.

My current Dockerrun.aws.json looks like this:

{
    "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"
}

This doesn't work however. When SSHing to my beanstalk instance then getting the content of the environment variables, I can see that they weren't initialized:

[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

How can I set the environment variables in my containers? The Dockerrun.aws.json doesn't seem to work for me.

Heschoon
  • 2,915
  • 9
  • 26
  • 55
  • 1
    Did you try this option: http://stackoverflow.com/questions/28298101/elastic-beanstalk-environment-variables-for-docker-host ? – Alex V Jul 01 '15 at 20:06
  • 1
    If you do not wish to keep the information on VCS, you can use the web console. It works well for my dockerized environments. http://stackoverflow.com/a/17878600/1102395 – Samar Jul 01 '15 at 21:10

2 Answers2

26

The "environment" field is not allowed in the Dockerrun.aws.json for single containers.

You can however specify the environment variables in a .config file with the following procedure (look at the documentation for more information):

  1. Create a folder .ebextensions
  2. Create a .config file in the folder
  3. Fill the config file:
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. Zip the .ebextensions file along with the Dockerrun.aws.json and upload it to Beanstalk
Mario S
  • 1,914
  • 1
  • 19
  • 32
Heschoon
  • 2,915
  • 9
  • 26
  • 55
  • what if you aren't bundling and instead is specifying an image on docker hub? Where would the `.ebextensions` go? – Luke Sep 16 '16 at 17:54
  • @Luke You still have to specify the Docker Hub image in a Dockerrun.aws.json file right? So create a git repository with your Dockerrun.aws.json and .ebextensions and then use the EB CLI to easily deploy the configuration. – Ian Walter Sep 24 '16 at 05:47
  • Is it somehow possible to use the directy environment variables created in the elastic beanstalk environment? – Anand Oct 15 '21 at 14:51
6

Another way (graphical):

In the configuration of your beanstalk click in modify of the Software card. In the bottom of this page you can fill the name and value of your environment variables.

  • 1
    I tried this and still the container cant access the environment variables defined directly on EBS. – Anand Oct 15 '21 at 14:52