1

I simply want to use a environment variable loaded from file in my docker-compose file. But after running the container, I only got

WARNING: The TESTVAR variable is not set. Defaulting to a blank string.

Only found this topic, but I'm using a later version of docker like there (docker-compose: 1.14.0, docker: 17.05.0-ce). And I changed the encoding to ISO 8859-1, since I found a github issue where strange behavior with encodings was detected. Both doesn't work.

My docker-compose file

version: '2'
services:
  mysql:
    container_name: test_${TESTVAR}
    build: mysql
    mem_limit: 1G
    env_file:
      - credentials.env

credentials.env contains only TESTVAR=test123. To start, I run docker-compose up mysql and I also tried to specify the environment variables directly in the compose file like this:

environment:
  - TESTVAR=1234

Not working, too.

Lion
  • 16,606
  • 23
  • 86
  • 148
  • 1
    Not sure what you are actually trying to achieve. Env variables will be available INSIDE the container by the container itself, not while running a docker-compose file. If you want that to work you have to export TESTVAR in your environment. – Mike Doe Feb 04 '18 at 12:20
  • Please look here - https://stackoverflow.com/questions/48495663/docker-compose-env-file-not-working – nickgryg Feb 04 '18 at 13:05

1 Answers1

0

If you want to use variables in the docker-compose.yml you can do it with .env file, docker docs

$ cat .env
TAG=v1.5
TESTVAR=123

$ cat docker-compose.yml
version: '3'
services:
  web:
    image: "webapp:${TAG}"
    environment: ["TESTVAR=${TESTVAR}"]
Mazel Tov
  • 2,064
  • 14
  • 26
  • 1
    you can have files common.env, prod.env, prod.private.env... but when deploying to production you will just copy the proper file to destination with name .env ... so the docker-compose.yml will always expect .env file but it is up to you to copy the proper file based on your ENV – Mazel Tov Mar 29 '23 at 09:34