2

I need to set environment variable in a running docker container. I am already aware of the way of setting environment variable while creating a container. As far I found there is no available straight forward way to do this with docker and docker is planning to add something with new version 1.13.

But I found that some people able to manage it which is not working for me now. I tried following ways but did not work for me-

docker exec -it -u=root test /bin/bash -c "export port=8090"

echo "export port=8090" to /etc/bash.bashrc using a script and then source it

docker exec -it test /bin/bash -c "source /etc/bash.bashrc"

configuring the whole thing in a script and run it from host also did not work. While running script from host all the other command successfully executes except "export port=8090" or "source /etc/bash.bashrc" or "source /root/.bashrc".

Can anyone explain why sourcing file from host does not work in docker container even when I set user("-u=root")? Can anyone help me to solve this? When I source the file from inside the container it works perfectly. But in my case I have to do it from host machine

NOTE:, I am using docker 1.12 and tried the above in ubuntu:16.04 and ubuntu:14.04

zaman sakib
  • 851
  • 11
  • 18
  • How do you know that the env is not set? Did you try `docker exec -it mydocker bash` and then once in the continer: `printenv`? Also, I'd try to first get into the container with the first command, then set the env there and run source. – cen Dec 27 '16 at 17:04
  • I go to the container (/bin/bash) and check the environment variables to determine the variable is there or not. I should have mentioned, in my case I cannot get inside the container to source the file. Forget to mention that sourcing from inside the container works perfectly. I have edited my question. – zaman sakib Dec 28 '16 at 00:52
  • One more thing you could try is to put the commands in a bash file, copy the file to container with docker and then execute that instead or have your application execute it. Based on your other comment it's probably a better idea to find some other way to configure your script. Dynamic configuration is not usually something that is done with env vars. – cen Dec 28 '16 at 07:21
  • Can you try to go into the container as the user which you used to set the environment variables: `docker exec -u root -it mydocker bash` – lvthillo Dec 28 '16 at 10:57
  • @cen Thank you. Actually I already mentioned that I put the command in a script and execute the script from host machine. Every command works fine except sourcing "bashrc" "bash.bashrc" or anything that tries to update environment variable. – zaman sakib Dec 28 '16 at 15:39

2 Answers2

1

If you have a running process in the docker and you are attempting to change the environment variable in the docker so the running process will dynamically change - this will not work. The environment variables of a process are set when it starts. You can see here ways to overcome that, but I don't think that is the right way to go.

I would instead, have a configuration file that the file reads (or listens to) periodically. And when you want to change the configuration change the file.

If this isn't your scenario, please describe your scenario so we can better assist you.

Community
  • 1
  • 1
Uri Shalit
  • 2,198
  • 2
  • 19
  • 29
  • There is no running process inside the container. In my case I know only part of the environment variables while creating the container. But later I get few more environment variable which are used in some scripts and this scripts will be run later from my application(which is running in the docker host machine) to start desired process inside container. – zaman sakib Dec 27 '16 at 21:45
  • @zamansakib A container can not run without a process. In your example you're running bash as process. – lvthillo Dec 28 '16 at 10:51
  • @lorenzvth7- you are right. I actually tried to say that no process running which is using the environment variable that I want to update. Thanks for pointing that out – zaman sakib Dec 28 '16 at 15:41
1

I find a way to provide environment variable to a running container. Fist upgrade your docker-engine. I am using V1.12.5.

create a script with environment variables-

#!/bin/bash

echo "export VAR1=VAL1
export VAR2=VAL2" >> /etc/bash.bashrc
source /etc/bash.bashrc

Now start a container. Here, 'test' is the container name:

docker run -idt --name=test ubuntu

Copy your script to container:

docker cp script.sh test:/

Run the script :

docker exec -it test /bin/bash -c "/script.sh"

Restart your container:

docker restart test 

Go to container shell

docker exec -it test /bin/bash

Check the variable

echo $VAR1
zaman sakib
  • 851
  • 11
  • 18