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 change ip address range of docker swarm ingress network
  • Recursive while loop in docker cmd
  • How do i bind Docker container to a specific external IP address, if i need to allocate many ports?
  • How to execute script during image build in Docker
  • Gulp unexpected token in linux not windows
  • Running ubuntu containers on ubuntu host - application with gui
  • 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.

  • How to configure docker-compose for storing data of Elastic search out-side of docker container?
  • Does 'docker run' modify image state?
  • Ansible docker_container module env file
  • Cannot connect to WCF service hosted in Docker container
  • Persistence of Docker Data Containers
  • Using docker with RDS (Prod), but confused about local DB setup (dev)
  • 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 VAR1=$VARZZZ > $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.