如何在运行的docker容器中设置一个环境变量 [英] How to set an environment variable in a running docker container

查看:4187
本文介绍了如何在运行的docker容器中设置一个环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个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:


  1. 创建自己的脚本,这将像你的命令的跑步者一样。例如:

  1. 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屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆