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:

  • .dockerignore all subfolders but not files in the folder itself
  • Using Docker I get the error: “SQLSTATE[HY000] [2002] No such file or directory”
  • docker-compose up Permission denied on Windows
  • Docker port binding not working as expected
  • Docker dependencies design for containers configuration and startup
  • composer install failing for unknown reason in travis-ci
  • version: '2'
          - ./.env
          - ./.env.${ENV}
          - ./.env.local
        image: postgres
          - ${POSTGRES_PORT}:5432

    .env looks like this:


    and the .env.development looks like this:


    .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.

  • Kubernetes with Vagrant nodes won't register
  • Spring Boot in Docker throwing an exception 'hibernate.dialect' not set
  • Running Gatling from docker-compose
  • Publisher Public key for docker notary
  • Are Docker links deprecated?
  • RabbitMQ cluster fails when one node is not reachable
  • One Solution collect form web for “docker-compose.yml with multiple env_files”

    while reading this page:

    and from my understanding, you should do the following:

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


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

        - 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.