How would I pass environment into docker using bash?

The following works fine in docker:

docker run -i -t -rm -e a="hello world" b=world ubuntu /bin/bash

What it does is passes env var a with value “hello world” and env var b with value “world” into the docker container.

  • How to configure spring interceptor to get called with every request
  • Any way to remove docker container and image automatically even after unsuccessful build?
  • Docker image dns probem, name solve failed
  • Docker is filling up my disk space
  • Installing R Language on Ubuntu 16 - unmet dependencies
  • web_1 | / line 99: exec: bundle: not found app_web_1 exited with code 127
  • Thing is, I need to generate that from script.

    It is super easy to get this working for env vars without space:

    ENV_VARS='-e a=helloworld b=world'
    docker run -i -t -rm $ENV_VARS ubuntu /bin/bash

    However, once there is a space in the env var I am hosed:

    ENV_VARS='-e a="hello world" b=world'
    docker run -i -t -rm $ENV_VARS ubuntu /bin/bash
    Unable to find image 'world"' (tag: latest) locally
    2014/01/15 16:28:40 Invalid repository name (world"), only [a-z0-9-_.] are allowed

    How can I get the above example to work? I also tried arrays but can not get them to work.

  • Docker container mongod error when starting via ssh
  • Unable to run docker node.js application
  • How to prevent RabbitMQ settings to be lost during intermediate steps of a Dockerfile?
  • Docker : Installing Docker behind a proxy on CentOS 7 : Error while pulling image
  • trying to start an image in docker
  • How to fail loudly during docker-compose up -d?
  • 4 Solutions collect form web for “How would I pass environment into docker using bash?”

    Bash arrays are designed to solve exactly this sort of problem

    First step is to declare the array:

    docker_env=(-e "a=hello world" "b=world")

    Which lets you programmatically populate more environment variables, for example:

    docker_env+=("c=foo bar")

    Finally run it:

    docker run -i -t -rm "${docker_env[@]}" ubuntu /bin/bash

    How about instead:

    a='hello world'
    b='some more'
    docker run -i -t -rm -e a -e b ...

    Does this do what you need in an eaiser way?

    eval docker run -i -t -rm "$ENV_VARS" ubuntu /bin/bash

    I solved problem of the space containing variables passed to docker environment using option –env-file. In the env file the line separated variable definitions are expected. Following example illustrates the pattern.

    DOCKER_ENV=$(mktemp run-docker-env.XXXXXXX)
    echo Using env file: $DOCKER_ENV
    echo VAR2=value >> $DOCKER_ENV
    echo VAR_WITH_SPACE=variable value with space >> $DOCKER_ENV
    docker run --env-file=${DOCKER_ENV} image:latest
    rm $DOCKER_ENV 
    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.