Compose: docker-compose doesn't use env_file

Created on 4 Oct 2016  路  9Comments  路  Source: docker/compose

$ docker-compose version
docker-compose version 1.8.1, build 878cff1
docker-py version: 1.10.3
CPython version: 2.7.9
OpenSSL version: OpenSSL 1.0.1e 11 Feb 2013

Here's how my docker-compose.yml file looks like:

parse-server:
  image: 94fc5293168d
  restart: always
  env_file: config.env
  ports:
    - "1337:1337"
  environment:
    PORT: 1337
    DATABASE_URI: $DATABASE_URI
    APP_ID: $APP_ID
    MASTER_KEY: $MASTER_KEY
 ......

However, when I run docker-compose up -d, Compose doesn't expose variables from config.env to environment variables.

Curious, when I remove env_file: config.env and rename config.env to .env, everything works just fine.

PS: env_file: ./config.env doesn't work either.

areconfig kinquestion

Most helpful comment

Environment variables have become unfortunately loaded and ambiguous when dealing with Compose. This is why we recently added a disambiguation page to our documentation that should help with those cases. If you have any suggestion to improve it however, we gladly welcome pull requests.

To summarize:

  • Variable substitution in your docker-compose.yml file will be pulled (in decreasing order of priority) from your shell's environment and your .env file.
  • Variables available in your container are a combination of values found in your env_file files and values described in the environment section of the service.
  • Those are two entirely separate sets of features.

HTH.

All 9 comments

I think that's exactly the behaviour the environment variable documentation describes. If you want to use the environment variables from config.env inside of the docker-compose.yml file, you have to use the .env file.

The way you're using the env_file parameter passes environment variables directly to the Dockerfile, where you can use them.

Please correct me if I'm wrong.. ;)

@robertaistleitner Looks like you right. Can anybody confirm this?
If env_file and .env are unrelated, it confuses people. See this:

The docs say you can specify you're own env-file or even multiple files, however I could not get that working. It always wanted to choose the .env file.

http://staxmanade.com/2016/05/how-to-get-environment-variables-passed-through-docker-compose-to-the-containers/

Environment variables have become unfortunately loaded and ambiguous when dealing with Compose. This is why we recently added a disambiguation page to our documentation that should help with those cases. If you have any suggestion to improve it however, we gladly welcome pull requests.

To summarize:

  • Variable substitution in your docker-compose.yml file will be pulled (in decreasing order of priority) from your shell's environment and your .env file.
  • Variables available in your container are a combination of values found in your env_file files and values described in the environment section of the service.
  • Those are two entirely separate sets of features.

HTH.

Are there any plans to allow specifying variable values in docker-compose command line?

You can easily add environment variables by defining them before the docker-compose command like this
ENVVAR=ENVVAL docker-compose up

@robertaistleitner: That's very useful, thanks for sharing it.

The use is as a list, not just 1 file:

my-service:
  env_file:
    - <env file>

@matthias-p-nowak The first example at https://docs.docker.com/compose/compose-file/compose-file-v2/#env_file has just one file, no list:

env_file: .env

original confusion returns

so wait. use case. I have a graphql.env file that configures the graphql port. And in docker-compose.yml I need to expose the port used my graphql, so what you're telling me is that I cannot use graphql.env env file to specify which port I want to expose, but I need yet another .env file and I need to maintain both of them and make sure they're both using the same PORT value in both of them just to make sure docker-compose gets it righ?

Have you guys gone insane?

Was this page helpful?
0 / 5 - 0 ratings