docker-compose.yml with multiple env_files

I’m trying to get a docker-compose file working with multiple .env files, and I’m not having any luck. I’m trying to setup three .env files:

  • global settings that are the same across all container instances
  • environment-specific settings (stuff just for test or dev)
  • local settings – overridable things that a developer might need to change in case they have conflicts with, say, a port number

My docker-compose.yml file looks like this:

  • Can we use docker-image with the hyperkit driver on a mac?
  • Running Docker as non-root user
  • docker error when using log-driver=“syslog” on OS X
  • Sharing volume between docker containers
  • Docker with vnc or vagrant with vnc for graphical aplications
  • Docker - unable to mount start mongodb in container - operation not permitted
  • version: '2'
    services:
      db:
        env_file:
          - ./.env
          - ./.env.${ENV}
          - ./.env.local
        image: postgres
        ports:
          - ${POSTGRES_PORT}:5432
    

    .env looks like this:

    POSTGRES_USER=myapp
    

    and the .env.development looks like this:

    POSTGRES_PASSWORD=supersecretpassword
    POSTGRES_HOST=localhost
    POSTGRES_PORT=25432
    POSTGRES_DB=myapp_development
    

    .env.local doesn’t exist in this case.

    After running ENV=development docker-compose up, I receive the following output:

    $ ENV=development docker-compose up
    WARNING: The POSTGRES_PASSWORD variable is not set. Defaulting to a blank string.
    WARNING: The POSTGRES_DB variable is not set. Defaulting to a blank string.
    WARNING: The POSTGRES_PORT variable is not set. Defaulting to a blank string.
    ERROR: The Compose file './docker-compose.yml' is invalid because:
    services.db.ports is invalid: Invalid port ":5432", should be [[remote_ip:]remote_port[-remote_port]:]port[/protocol]
    

    From that error message, it looks like none of my environment variables are being used. I just upgraded to the newest available docker-compose as well – same errors:

    $ docker-compose --version
    docker-compose version 1.8.0-rc1, build 9bf6bc6
    

    Any ideas here? Would be nice to have a single docker-compose.yml that would work across multiple environments.

  • Docker example for frontend and backend application
  • nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) on docker
  • Nginx dockerized accessing host IIS website on a given port
  • Constraint in docker-compose gives container not found error in ucp
  • Does putting ARG at top of Dockerfile prevent layer re-use?
  • Getting “netcoreapp1.0” is an unsupported framework during Docker build
  • One Solution collect form web for “docker-compose.yml with multiple env_files”

    while reading this page: https://docs.docker.com/compose/environment-variables/

    and from my understanding, you should do the following:

    for the global variables(that should not change) make an env file like so:

    VAR1=VALUE1
    VAR2=VALUE2
    

    and for the others(that might change) you should add their name under environment in docker-compose.yml like this:

    environment:
        - VAR1
        - VAR2
    

    this will take the VAR1 and VAR2 values from the shell you are running docker-compose.

    I hope this helps.

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.