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:

  • Docker for Mac with WebStorm Node Remote Interpreter
  • Mattermost “An existing user is already attached to your gitlab account”
  • Creating docker-consul official image
  • How can I curl 127.0.0.1/8000 while django development server is running?
  • Running commands in a Docker file
  • Restrict memory across all 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.

  • Why I need a password to get access to the jupyter notebook when I use docker machine?
  • fish shell pass argument from command
  • What is the difference between running an application on ec2-server and running an application on top of docker on ec2-server?
  • Are there any simple python packages that will run a prebuilt docker image
  • Local hostnames for Docker containers
  • Development environment setup for Mac and CentOS using Docker
  • 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.