1

I have a requirement to set the environment variable for docker container at runtime. The value can only be determined at runtime by combination two other variables which are available at runtime. In a simpler form, the following is not working.

docker run -ti -e host="name" -e port="123" centos:7 bash -c "export url=$host:$port; env"

returns the following where url is empty when the value of $host and $port is available to construct $url ?

...
host=name
url=:
port=123
...

1 Answer 1

2

You have to single quote the shell command to avoid your interactive shell expanding the variable before the docker shell sees them:

docker run -ti -e host="name" -e port="123" centos:7 bash -c 'export url=$host:$port; env'

With single quotes, your shell will pass export url=$host:$port; env to Docker.

With double quotes, your current shell will first dutifully expand the variables, and therefore just pass url=:; env

Sign up to request clarification or add additional context in comments.

1 Comment

this works. I thought my question had an example of simplified form of problem that I am facing. Actually, I have a dockerfile with a custom entrypoint. This environment variable $host should be available in container. I can confirm from the logs that the $url is set during the start up of container. But when I bash into the container, this variable is empty.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.