如何在正在运行的 docker 容器中设置环境变量 [英] How to set an environment variable in a running docker container
问题描述
如果我有一个不久前启动的 docker 容器,在该运行的容器中设置环境变量的最佳方法是什么?当我运行 run 命令时,我最初设置了一个环境变量.
If I have a docker container that I started a while back, what is the best way to set an environment variable in that running container? I set an environment variable initially when I ran the run command.
$ docker run --name my-wordpress -e VIRTUAL_HOST=domain.com --link my-mysql:mysql -d spencercooley/wordpress
但是现在它已经运行了一段时间,我想将另一个 VIRTUAL_HOST
添加到环境变量中.我不想删除容器,然后只用我想要的环境变量重新运行它,因为这样我就必须将旧卷迁移到新容器,它有我没有的主题文件和上传想输.
but now that it has been running for a while I want to add another VIRTUAL_HOST
to the environment variable. I do not want to delete the container and then just re-run it with the environment variable that I want because then I would have to migrate the old volumes to the new container, it has theme files and uploads in it that I don't want to lose.
我只想更改VIRTUAL_HOST
环境变量的值.
I would just like to change the value of VIRTUAL_HOST
environment variable.
推荐答案
一般有两种选择,因为docker现在不支持这个功能:
There are generaly two options, because docker doesn't support this feature now:
创建您自己的脚本,该脚本将作为您的命令的运行器.例如:
Create your own script, which will act like runner for your command. For example:
#!/bin/bash
export VAR1=VAL1
export VAR2=VAL2
your_cmd
按照以下方式运行您的命令:
Run your command following way:
docker exec -i CONTAINER_ID /bin/bash -c "export VAR1=VAL1 && export VAR2=VAL2 && your_cmd"
这篇关于如何在正在运行的 docker 容器中设置环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!