Docker-compose not seeing environment variables on the host

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

I was having an issue with a variable in the .env file not being substituted in the compose.yml when I found this thread. None of the solutions above worked, but I did realize that I had a - character in my environment variable. (Not as a value, but in the actual variable name).

When I replaced the - character with a _ character in my variable name, it worked as expected. Looking back, that’s a pretty obvious syntax error on my part, but I am sharing it here, just in case it helps someone else!