0

how can i access to my os env variable in docker compose?

in my os:

export TOKEN=""
sudo docker-compose up
# WARNING: The TOKEN variable is not set. Defaulting to a blank string.

docker-compose.yml

version: '3.8'

services:
  web:
    build:
      context: ../
      dockerfile: build/Dockerfile
    environment:
       - TOKEN=$TOKEN
  • 2
    Hi, please *Do Not* post your code as an image, post is as a text instead. [Here is why](https://meta.stackoverflow.com/a/285557/15366635) – I_love_vegetables Sep 14 '21 at 08:17
  • 1
    Does running `sudo -E docker-compose up` help? `sudo` normally discards environment variables, but see [How to keep environment variables when using sudo](https://stackoverflow.com/questions/8633461/how-to-keep-environment-variables-when-using-sudo). – David Maze Sep 14 '21 at 10:03

2 Answers2

0

It looks about right, environment variables you export in your shell for the compose process to be able to grab should be available to it. If you're having issues maybe changing one of the names would suffice?

I'm linking a thread from the docker forums on the issue

Noam Yizraeli
  • 4,446
  • 18
  • 35
0

you can use

export ENV_VARIABLE=""
version: '3.8'

services:
  web:
    build:
      context: ../
      dockerfile: build/Dockerfile
    environment:
       - ENV_VARIABLE=$ENV_VARIABLE

your mistake was in using sudo !

just run docker-compose up

  • Since you could specify `volumes: [/:/host], user: root` in the Compose configuration to get read-write access to the entire host filesystem, it's very reasonable to require `sudo` to use Docker. – David Maze Sep 14 '21 at 10:02