-
Notifications
You must be signed in to change notification settings - Fork 18.6k
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
How to set an enviroment variable on an existing container? #8838
Comments
I am looking for that as well, |
@Boran @antonbormotov I believe commit is the right way here since you essentially want to keep a modified state of a container. If your program is smart enough to have a way to source some file upon being notified, you could /cc @jfrazelle @tianon |
The container name and and hostname within are also cast in stone. Edit: when doing a commit - rm - run, one loses the contents of non bound data volumes, which I just found out the hard way. I guess it is a conceptual issue that one just has to live with. |
I find it very usefull. +1 |
Yes. It will be very useful. At the moment, my workaround is this: docker exec -i CONTAINER_ID /bin/bash -c "export VAR1=VAL1 && export VAR2=VAL2 && run_cmd" |
@bletvaska How does that work? |
to @onbjerg before the command |
But will this update the env variables "forever"? I mean, if I run this and then 5 days later change some variable in run_cmd, will it still work? Or is it a one-off command? |
@onbjerg it is a one-off command. if you want to make this change permanent in the container, you have to commit your changes as was mentioned earlier. |
I found a solution... You just stop docker daemon and change container config in
I also tried to do this with just docker container reseting but file seems to be rewriten on each start. This is just a temporary solution (I hope), since, I do not want to stop all containers just to modify one... You can find container-id by executing
|
didn't work for me.
|
You did not stop docker daemon before editing config and start it after. :) |
Yes, change an env variable works, confirmed. |
This is a very annoying limitation |
For those interested, there's an open PR for changing properties of running/existing containers, but it has not yet been decided on; #15078 |
Thanks /var/lib/docker/containers/[container-id]/config.json |
Folks, Let's close the question. Here is my challenge: I didn't find the answer easily ... but here it is!
Cheers! |
Now the config file is overwritten even after docker is stopped. :( |
I do not understand why this issue is closed? "I guess it is a conceptual issue that one just has to live with." is not a closing argument either.
I would answer the second question with yes, so one should not close this issue from my point of view. |
ENV variables are not meant to be changed. |
For me it's ok to close this, it's a conceptual issue. |
docker exec -ti -e LINES=$LINES -e COLUMNS=$COLUMNS $container bash |
The problem is that when you stop a container it actually kills all the processes, than when you do start it run the processes again from scratch, reading the old env values from somewhere, why can't I override them? @nemanjan00 this is IMO a pure technical issue and there are some cases when commit to image and run again as new container is very cumbersome and annoying. |
I've encountered this roadblock several times. For the more less experienced programmers, docker is a great tool, but I wish containers/configurations were more pliable on-the-fly. In this case, setting environment variables for an existing container. Thanks. |
I Just encountered this issue for the nth time. It does seem like a pain but thinking about it, your containers shouldn't contain any state that is not entirely disposable, so docker is helping us help ourselves towards a goal of immutability and documented deploys. I Suppose it depends what you are using docker for, but I imagine there would be many that would be horrified if their ENV variable for database was changed during runtime without notice to another DB siphoning off user-data. Just thought I'd put that out there. |
Is there a method like kube's edit or something to edit docker inspect ? |
@pykiss Thanks for the solution. It is working! |
I can't find /var/lib/docker/.....
but when i cd to this location , not found location
What's wrong with me? |
@ntlzz93 hosts is probably a file a not a folder |
@Boran you can use bash script in entrypoint and receive environments from env file or other your best way, on every start, restart container, entrypoint updates your enviroments.
in bash.sh
im using CMD in dockerfile
|
I know this issue is closed since a while but I'm surprised. Has anyone ever considered using a .profile to solve this? In case some people still come on this issue which is on Google Search Top10 results, we use .profile here. (See details in this issue: #22490 (comment)) |
I don't know how |
the argument tho is that this shouldn't be the case. you should not have to offline a container completely to update its configuration. its a config file. config files should be adjustable for specific circumstances as needed in my opinion |
There are plenty of good reasons why variables can not be universally changed in the runtime:
The proper way to change env vars is to deploy new version, by altering docker-compose.yml/dockcer-stack.yml or whatever other mechanism you use for deploying application |
One possibility is to allow changing environment variables, but they would only be effective on container restart. |
It would be more of rebuild than restart, since restart does persist storage... |
但是我的容器已经启动,该怎样修改启动容器时的环境变量呢#首先停止容器
docker stop gitlab
#获取到容器ID
export CONTAINER_ID=`docker inspect --format="{{.Id}}" gitlab`
#检查获取的是否正确
echo $CONTAINER_ID
#修改容器配置所在的位置(默认位置是 sudo vim /var/lib/docker/containers/$CONTAINER_ID/config.v2.json)
sudo vim /media/yangyile/SSDYILE/dockervolume/docker/containers/$CONTAINER_ID/config.v2.json
#在里面搜一下就能找到 "Env" 的配置,编辑即可
#编辑完重启docker服务
sudo systemctl stop docker
sudo systemctl stop docker.socket
sudo systemctl start docker.socket
sudo systemctl start docker
#容器再次启动后验证结果
docker exec gitlab bash -c 'echo $GITLAB_OMNIBUS_CONFIG'
#很明显结果已经生效,而看启动日志也是正常的启动
docker logs --tail 100 gitlab
#感谢这个教程
#https://stackoverflow.com/questions/27812548/how-to-set-an-environment-variable-in-a-running-docker-container
#里面的某个人的指导意见恰好能用于我的情况 |
One can pass environment vars into new containers when building them with "docker run":
docker run -e "FOO=bar" ...
But how can one do that for an existing container, there is no -e option for docker run.
I wish to be able to restart a container with a new environment setting, but do not wish to actually recreate the container (as it has lots of manual changes in there).
One option might be to commit the container to an image and create a new container based on that image. Seems a very roundabout way though.
have I missed something?
The text was updated successfully, but these errors were encountered: