简体   繁体   English

AWS上的Docker-环境变量未从主机继承到容器

[英]Docker on AWS - Environment Variables not inheriting from host to container

I have a script (hosted on GitHub ) that does the following: 我有一个脚本(托管在GitHub上 ),可以执行以下操作:

  • Creates an EC2 instance on AWS 在AWS上创建EC2实例
  • Saves the local IP (private IP address) as an environment variable $LOCALIP 将本地IP(专用IP地址) $LOCALIP为环境变量$LOCALIP
  • Installs Docker (official repo) 安装Docker(官方仓库)
  • Updates the base instance (Ubuntu 16.04 LTS) 更新基础实例(Ubuntu 16.04 LTS)
  • Pulls a custom image of mine 拉出我的自定义图像
  • Runs said image with the -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.

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