1
docker run -e HOST_IP:xxxx

And I use the cmd to check if env HOST_IP changed:

docker exec CONTAINER_ID env |grep HOST_IP 

I found it not changed after these tries:

docker exec CONTAINER_ID bash -c 'export HOST_IP=XXX'
docker exec CONTAINER_ID bash -c 'echo HOST_IP=XXX >> ~/.bashrc && source ~/.bashrc'

Why? What controlled the env variable?

Thomas Orlita
  • 1,554
  • 14
  • 28
whi
  • 2,685
  • 6
  • 33
  • 40

2 Answers2

0

It may just be that you need to use the form

docker run -e NEW_ENV_VAR='new_var_val'

as mentioned in: How to pass environment variables to docker containers?

you can check by issuing the env command to the running container:

sudo docker exec -it running-container-name env
Community
  • 1
  • 1
errata
  • 23,596
  • 2
  • 22
  • 32
-1

Docker exec runs your command in a new session (bash or sh) every time and thus your exported environment variables are gone on next execution.

If you want to persistently change environment variables in Docker containers runtime, the only way I can think of is to copy your variables in /etc/environment and bash will import them if it's configured to do so, or alternatively you can export HOST_IP=XXX in .bashrc for the same effect.

But remember in this scenario your application inside docker still is not able to pick it up if it does not use bash at start up. In this case you need to make your app to read variables from a file or similar.

Boynux
  • 5,958
  • 2
  • 21
  • 29
  • Thanks for the explain. But most of my containers are customized by entry_point.sh or started by supervisor .e.g. /bin/bash is not used. – whi Jan 01 '16 at 04:26
  • `entry_point.sh` is a shell script, isn't it? So obviously it spawns a new shell. In supervisor case it depends on your service configuration, it might spawn a shell as well. – Boynux Jan 01 '16 at 04:29
  • So what you can do, is to add new env vars in `entry_point.sh` or supervisor service config. (and restart service) But you can't just execute a command and `export` and new var and it appears in other sessions, it's not. – Boynux Jan 01 '16 at 04:31
  • not understand yet: i use ssh to the container, should be the bash session. but whatever i tried to change the varaible and restart by supervisorctl, env just not changed. – whi Jan 01 '16 at 05:17
  • SSH? In original question doesn't seem to use SSH. – Boynux Jan 03 '16 at 04:35