-1

I am aware that we can specify the option -e during the run command to set environment variables in a docker. This only sets the PATH for the root user. Let us say if I have another user called admin and want to set the environment variables for that user as well, how can I achieve that?

This is the command I tried to set environment variables.

docker run -t -d -v /usr/hdp:/usr/hdp -v /usr/lib/jvm/:/usr/lib/jvm/ -e JAVA_HOME="${java_home}" -e HADOOP_HOME="${hadoop_home}" -e PATH=$PATH:$JAVA_HOME/bin -e PATH=$PATH:$HADOOP_HOME/bin gtimage

This only sets the PATH under root user but not for my admin user which a software that I installed during docker build has created.

jww
  • 97,681
  • 90
  • 411
  • 885
Atom
  • 768
  • 1
  • 15
  • 35
  • what's wrong with the question? Downvoter.. care to comment? – Atom Apr 18 '18 at 20:29
  • [Why when I switch to a different user environment variable is lost?](https://stackoverflow.com/q/36978880/608639), [How do I pass environment variables to Docker containers?](https://stackoverflow.com/q/30494050/608639), [Can I use an environment variable in a Dockerfile USER statement?](https://stackoverflow.com/q/48958477/608639), etc. – jww Apr 19 '18 at 01:52

1 Answers1

0

I don't have a perfect solution for my question above but I tried something like below to login as user and set environment variables for that user. I don't recommend the below way unless you could not find a solution for your problem. Please let me know if you find a better approach than this

docker exec $containervalue bash -c 'env | grep PATH >> temp && chmod 775 temp && mv temp /opt/nagios'
docker exec --user ngadmin $containervalue bash -c 'cat ~/temp >> ~/.bashrc &&  source ~/.bashrc'
Atom
  • 768
  • 1
  • 15
  • 35