[英]Docker on AWS - Environment Variables not inheriting from host to container
I have a script (hosted on GitHub ) that does the following: 我有一个脚本(托管在GitHub上 ),可以执行以下操作:
$LOCALIP
将本地IP(专用IP地址) $LOCALIP
为环境变量$LOCALIP
-e LOCALIP
trying to pass the hosts environment variable to the container (I have also tried -e LOCALIP=$LOCALIP
使用-e LOCALIP
运行所述图像,尝试将主机环境变量传递给容器(我也尝试过-e LOCALIP=$LOCALIP
However when I docker exec
into the container on that instance and run echo $LOCALIP
it displays nothing. 但是,当我在该实例上的容器中执行echo $LOCALIP
docker exec
并运行echo $LOCALIP
它什么也不显示。 Running env
shows me that LOCALIP
is there but nothing is against it 运行env
告诉我LOCALIP
在那里,但是没有反对的地方
If I destroy the container and remake using the exact same line from the original script (with -e LOCALIP=$LOCALIP
) it works - I need this process automating however and some additional help would be greatly appreciated. 如果我销毁容器并使用原始脚本中的完全相同的行(带有-e LOCALIP=$LOCALIP
)重制,则它可以正常工作-我需要使该过程自动化,但是,一些其他帮助将不胜感激。
Essentially sudo docker run -dit -e LOCALIP -p 1099:1099 -p 50000:50000 screamingjoypad/armada-server /bin/bash
is not sharing the hosts LOCALIP
variable. 本质上sudo docker run -dit -e LOCALIP -p 1099:1099 -p 50000:50000 screamingjoypad/armada-server /bin/bash
不共享主机LOCALIP
变量。
UPDATE 更新
Trying the suggestions from below I added the following line to my script source /etc/bash.bashrc
but this still does not work. 从下面尝试建议,我source /etc/bash.bashrc
下行添加到脚本source /etc/bash.bashrc
但这仍然无法正常工作。 I'm still getting a blank when trying the echo $LOCALIP
in the container... 尝试在容器中echo $LOCALIP
时,我仍然一片空白...
The problem is because of this line of your shell script: 问题是由于shell脚本的这一行:
echo "export LOCALIP=$(hostname -i)" >> /etc/bash.bashrc
After this command is executed, the export
instruction does not take effect yet. 执行该命令后, export
指令尚未生效。 It will take effect after your next login. 下次登录后生效。
To make the LOCALIP
environment variable take effect immediately, add this line after the echo "export ...
command: 要使LOCALIP
环境变量立即生效,请在echo "export ...
命令之后添加以下行:
source /etc/bash.bashrc
I believe I've solved it. 我相信我已经解决了。 I'm now using the AWS metadata to harvest the private IP address using the following addition to the script: 我现在正在使用AWS元数据通过脚本中的以下补充来获取私有IP地址:
sudo docker run -dit -e LOCALIP=$(curl http://169.254.169.254/latest/meta-data/local-ipv4) -p 1099:1099 -p 50000:50000 screamingjoypad/armada-server /bin/bash
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.