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.

  • docker behind proxy doesn't use system proxy info
  • How to link to additional container without restarting?
  • Get YAML for deployed Kubernetes services?
  • Docker Container starting with mongo server
  • Docker Network Namespace
  • Makefile - append to command
  • 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.

  • Why can't I curl one docker container from another via the host
  • Docker volume conflict
  • Docker command won't connect to docker
  • docker container not able to write on host machine
  • Docker build to run Dart app can't find pubspec.yaml
  • How to change container configuration without Dockerfile?
  • 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.