简体   繁体   中英

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

I have a script (hosted on GitHub ) that does the following:

  • Creates an EC2 instance on AWS
  • Saves the local IP (private IP address) as an environment variable $LOCALIP
  • Installs Docker (official repo)
  • Updates the base instance (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

However when I docker exec into the container on that instance and run echo $LOCALIP it displays nothing. Running env shows me that LOCALIP is there but nothing is against it

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.

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.

UPDATE

Trying the suggestions from below I added the following line to my script source /etc/bash.bashrc but this still does not work. I'm still getting a blank when trying the echo $LOCALIP in the container...

在此处输入图片说明

The problem is because of this line of your shell script:

echo "export LOCALIP=$(hostname -i)" >> /etc/bash.bashrc

After this command is executed, the export instruction does not take effect yet. 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:

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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