In my case I have defined environment variable in /etc/profile (Debian 10) like this:
MY_VAR=tom
export MY_VAR
And (after rebooting or source /etc/profile
) was able to see it’s value - yet not in the docker-compose.
Checking ${USER} reminded me, that container is running as root user, which lead me to check, that this variable will not be seen in root user without shell login - linux - Adjusting $PATH in /etc/profile does not affect root - Super User
So the solution is to use one of the command to simulate root shell login (look on superuser link above) for example sudo -i <command>
or to run container as different user, for example with user:
in docker-compose or with --user
in docker run - Compose file version 3 reference | Docker Documentation