Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would I pass environment into docker using bash?

Tags:

bash

docker

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.

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.

like image 962
Sam Saffron Avatar asked Jan 16 '14 00:01

Sam Saffron


3 Answers

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
like image 91
Michael Kropat Avatar answered Oct 09 '22 10:10

Michael Kropat


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?

like image 2
tianon Avatar answered Oct 09 '22 09:10

tianon


eval docker run -i -t -rm "$ENV_VARS" ubuntu /bin/bash
like image 1
William Pursell Avatar answered Oct 09 '22 10:10

William Pursell