在运行 docker contianer 中设置环境变量 [英] set environment variable in running docker contianer

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

问题描述

我需要在正在运行的 docker 容器中设置环境变量.我已经知道在创建容器时设置环境变量的方式.到目前为止,我发现没有可用的直接方法来使用 docker 执行此操作,并且 docker 计划在新版本 1.13 中添加一些内容.

I need to set environment variable in a running docker container. I am already aware of the way of setting environment variable while creating a container. As far I found there is no available straight forward way to do this with docker and docker is planning to add something with new version 1.13.

但我发现有些人能够管理它,这对我现在不起作用.我尝试了以下方法,但对我不起作用-

But I found that some people able to manage it which is not working for me now. I tried following ways but did not work for me-

docker exec -it -u=root test /bin/bash -c "export port=8090"

使用脚本将export port=8090"回显到/etc/bash.bashrc,然后获取它

echo "export port=8090" to /etc/bash.bashrc using a script and then source it

docker exec -it test /bin/bash -c "source /etc/bash.bashrc"

在脚本中配置整个内容并从主机运行它也不起作用.从主机运行脚本时,除export port=8090"或source/etc/bash.bashrc"或source/root/.bashrc"外,所有其他命令均成功执行.

configuring the whole thing in a script and run it from host also did not work. While running script from host all the other command successfully executes except "export port=8090" or "source /etc/bash.bashrc" or "source /root/.bashrc".

谁能解释为什么即使我设置了用户(-u=root"),从主机获取文件也不能在 docker 容器中工作?谁能帮我解决这个问题?当我从容器内部获取文件时,它可以完美运行.但就我而言,我必须从主机上完成

Can anyone explain why sourcing file from host does not work in docker container even when I set user("-u=root")? Can anyone help me to solve this? When I source the file from inside the container it works perfectly. But in my case I have to do it from host machine

注意:,我正在使用 docker 1.12 并在 ubuntu:16.04 和 ubuntu:14.04 中尝试了上述操作

NOTE:, I am using docker 1.12 and tried the above in ubuntu:16.04 and ubuntu:14.04

推荐答案

我找到了一种方法来为正在运行的容器提供环境变量.拳头升级你的码头引擎.我正在使用 V1.12.5.

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

现在启动一个容器.这里,'test' 是容器名称:

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:/

运行脚本:

docker exec -it test /bin/bash -c "/script.sh"

重启你的容器:

docker restart test 

转到容器外壳

docker exec -it test /bin/bash

检查变量

echo $VAR1

这篇关于在运行 docker contianer 中设置环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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