Docker-compose not seeing environment variables on the host

I figured it out, I’m posting this in case someone might need it.
My problem was that the environment variables are not preserved by default when sodoing.
So, in order for compose variable substitution to work, when the user is not added to docker group, you must add the -E flag to sudo.
For the example above:

$ export FOO=bar
$ sudo -E docker-compose up
Creating network "example_default" with the default driver
Creating example_test_1 ... 
Creating example_test_1 ... done
Attaching to example_test_1
test_1  | bar
example_test_1 exited with code 0

ps. docker-compose config really helped me debug this, it shows in the shell the result of the variables substitutions in docker-compose.yml.

6 Likes