set environment variable in running docker contianer

一世执手 提交于 2019-12-02 03:50:20
Uri Shalit

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.

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!