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?