Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing environment variables to Docker containers

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.

like image 460
Heschoon Avatar asked Jul 01 '15 15:07

Heschoon


People also ask

How do I run an environment variable in a docker container?

When we launch our Docker container, we can pass environment variables as key-value pairs directly into the command line using the parameter –env (or its short form -e). For instance, let's execute the following command: $ docker run --env VARIABLE1=foobar alpine:3 env

How do I Pass environment variables to a container?

You can pass environment variables to your containers with the -e flag. Or, if you don't want to have the value on the command-line where it will be displayed by ps, etc., -e can pull in the value from the current environment if you just give it without the =: sudo PASSWORD='foo' docker run [...] -e PASSWORD [...]

How do I run an ENV file in Docker using Bash?

docker run --rm -it --env-file < (bash -c 'env | grep <your env data>') Is a way to grep the data stored within a .env and pass them to Docker, without anything being stored unsecurely (so you can't just look at docker history and grab keys.

How do I run a Postgres Postgres variable in Docker?

The command used to launch Docker containers, docker run, accepts ENV variables as arguments. Simply run it with the -e flag, shorthand for --env, and pass in the key=value pair: sudo docker run -e POSTGRES_USER='postgres' -e POSTGRES_PASSWORD='password' ...


1 Answers

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
like image 131
2 revs, 2 users 75% Avatar answered Sep 28 '22 22:09

2 revs, 2 users 75%