Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

setting environment variables in a docker container #25388

Closed
patricxu opened this issue Aug 4, 2016 · 2 comments
Closed

setting environment variables in a docker container #25388

patricxu opened this issue Aug 4, 2016 · 2 comments

Comments

@patricxu
Copy link

patricxu commented Aug 4, 2016

Hi folks,

I am trying to add a environment variable, TESTVAR, into /etc/environment or /etc/profile in my Debian 8 container. The files was successfully modified. Then

  1. commit the container to the image
  2. stop the container
  3. run a new container from the newly committed image
  4. docker exec -u 0 -it bash.
    I have noticed that the file /etc/environment or /etc/profile has changed, but echo $TESTVAR outputs nothing.
    I am confused why the above steps did not work. Can anybody help? Thanks!
@thaJeztah
Copy link
Member

Try setting them in /etc/bash.bashrc

However, the standard way to set environment variables, is to use the -e option on Docker run, or set ENV inside a Dockerfile

I'll close this issue, because this is a question, not a bug, and our issue tracker is not intended as a general support forum (better ask in forums.docker.com, or in the #docker IRC channel), but I hope this helps

@bacongobbler
Copy link

For anyone else attempting this, setting them in /etc/bash.bashrc doesn't work either. You'll have to explicitly source the file you've written to in order to get those environment variables available for your command/script/whatnot, e.g. docker run mycontainer bash -c 'source /etc/environment && env'.

However, the standard way to set environment variables, is to use the -e option on Docker run, or set ENV inside a Dockerfile

Just to clarify our use case, there are situations where we need to set envvars after the container has started rather than at compile time through the CLI, so neither of those solutions will work for us. For example, setting an envvar based on the container's hostname.

Hope that helps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants