简体   繁体   English

Docker:如何在不重启Docker容器的情况下永久更改env var值

[英]Docker : how to change permanantely an env var value inside a docker container without restarting it

I have a docker container which is already started : 我有一个已经启动的docker容器:

When i run : docker exec -it myContainer bash 当我运行时: docker exec -it myContainer bash

and then echo $myEnvVar -> i get : myEnvVar= value1 然后echo $myEnvVar >我得到: myEnvVar= value1

i'm trying to change the value of this env var : 我正在尝试更改此env var的值:

i ve tried to do : export myEnvVar=value2 inside the container 我试着做:在容器内export myEnvVar=value2

and from the host server : docker exec -it myContainer bash -c "export myEnvVar =value2" 并从主机服务器: docker exec -it myContainer bash -c "export myEnvVar =value2"

or alson another way : docker exec -it -e myEnvVar =value2" myContainer bash 或也以另一种方式: docker exec -it -e myEnvVar =value2" myContainer bash

All those tentatives fails as it is setting this new value2 o nly in the current session 所有这些尝试都失败了,因为它在当前会话中仅设置了此新值2

Therofore when i exit from the container and reconnect to it , 因此,当我从容器中退出并重新连接到它时,

i found that my var is still equal to the first value myEnvVar= value1 我发现我的var仍然等于第一个值myEnvVar= value1

Suggestions to handle it without restarting the container ? 建议在不重新启动容器的情况下处理它?

not a docker guy but found some useful thread, may be it will help. 不是码头工人,但发现了一些有用的线程,可能会有所帮助。

follow the thread: https://github.com/moby/moby/issues/8838 遵循线程: https : //github.com/moby/moby/issues/8838

You can try one approach: just stop docker daemon and change container config in 您可以尝试一种方法:仅停止docker daemon并在以下位置更改容器配置

/var/lib/docker/containers/[container-id]/config.json

You can find container-id by executing 您可以通过执行以下命令找到容器ID

docker inspect [container-name]

Environment variables are defined on a process when you create the process, and a running docker container is a wrapper around a running process. 创建进程时,在进程上定义环境变量,并且正在运行的docker容器是正在运行的进程的包装。 Therefore, it's not possible to achieve your request even if docker wanted to make it possible. 因此,即使Docker希望实现您的请求,也无法实现。

Your request also goes against the best practices to make your containers disposable and replaced to implement changes. 您的要求还违反了使容器可丢弃并更换以实施更改的最佳做法。 By modifying an existing container, you are creating a fragile "pet", rather than a fault tolerant "cattle" environment. 通过修改现有容器,您将创建一个脆弱的“宠物”,而不是容错的“牛”环境。

The recommended solution to changing an environment variable for a container is to replace the container with a new container that has the desired environment variable defined. 更改容器环境变量的建议解决方案是用定义了所需环境变量的新容器替换该容器。 When you have lots of settings to run your containers, consider using a docker-compose.yml file and deploying your containers with docker-compose or docker stack deploy . 如果您有许多要运行容器的设置,请考虑使用docker-compose.yml文件,并使用docker-compose或docker stack deploy容器。 This makes it easy to define the new settings in a config file and replaces the needed containers with a single "up" or "deploy" command. 这样可以轻松地在配置文件中定义新设置,并用一个“ up”或“ deploy”命令替换所需的容器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM