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:

  • godep vendor with docker
  • Run an ecosystem in docker
  • How can I debug my docker with PHPStorm
  • After deployment of WAR to tomcat docker container always getting 404
  • how to detach from a docker container
  • How persistent are docker data-only containers
  • 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.

  • How to deploy a self-building Docker image to make changes to itself in respect to the local environment?
  • Calling OpenConnect VPN client in docker container shows TUNSETIFF failed: Operation not permitted
  • Saving database state from library/postgres
  • Multi PostgreSQL DB in data only container
  • How to have default file in image that I can override during container run?
  • Restricting access to mounted /var/run/docker.sock
  • 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.