0

Trying to understand something that I found interesting and confusing about host environment variable/paths. Couldn't find anything resembling this and I don't have enough CLI experience to know the right wording so hope you lot can help.

I was setting up an application that requires reading an environment variable that is set in .bashrc file.

export THIS_PATH=/this/location

Made sure to make it permanent. The variable then gets read in a docker-compose file for its location mapping.

"$THIS_PATH/folder:/etc/folder"

Running the command sudo docker-compose -f docker-compose.yml up -d yields an error going along the lines of

WARN[0] The variable THIS_PATH is not set. Default to blank string

Stumped me quite a while but I managed to resolve it by adding the user to the docker group, so I ran the command without getting any errors.

Curiously I used printenv with and without sudo to see the difference, and to my surprise the PATH wasn't in sudo.

Question

Is there a difference in environment paths between non-sudo and sudo? Can anyone explain to me about this?

I may have my terms wrong here and there. Hope you guys can correct me. Thank you in advance!

Edit #1

Maybe I have to rephrase it.

Why the need for sudo -E if the environment variable is already in the non-sudo environment? Why doesn't sudo just use the already existing environment? Why the need of separation?

  • Does https://stackoverflow.com/questions/8633461/how-to-keep-environment-variables-when-using-sudo answer your question? – KamilCuk Jan 18 '23 at 08:25
  • In fact, `sudo` completely resets the environment; `sudo -E` will keep most variables, as described in the question @KamilCuk cites. A Compose file can also interpret relative paths in most contexts, which could get around needing this environment variable. – David Maze Jan 18 '23 at 12:23
  • Hi. Sorry for the late reply. Thanks for the thread @KamilCuk and @David Maze. I understand using ```sudo -E``` solves it. Just wondering why the separation since my understanding that ```sudo``` would use the environment as is and not reset it. Maybe my understanding is still limited about that. – kashfil_aziz Feb 01 '23 at 06:25

0 Answers0