skip to Main Content

I have a profile in my Compose file like:

  web_prod_db:
    <<: *web
    command: etc. etc.
    env_file:
      - .env
      - .env.prod-db
    profiles:
      - myprofile

I want the values in .env.prod-db to override the (same-named) variables in .env, but this doesn’t seem to happen – even if I swap the order that the files are specified so .env.prod-db is specified first.

Is there anything I can do here to ensure the overrides happen as desired?

2

Answers


  1. To override values, you can use this approach: https://docs.docker.com/compose/extends/

    You can create your main docker-compose.yml file, and others dockerfiles that will override values that you desired. As an example:

    docker-compose.yml

    web:
      image: example/my_web_app:latest
      depends_on:
        - db
        - cache
    
    db:
      image: postgres:latest
    
    cache:
      image: redis:latest
    

    docker-compose.prod.yml

    web:
      ports:
        - 80:80
      environment:
        PRODUCTION: 'true'
    
    cache:
      environment:
        TTL: '500'
    

    And run the command:

    docker-compose -f docker-compose.yml -f docker-compose.prod.yml up -d
    

    That will run your containers and will override/merge the values from docker-compose.yml with the values defined in docker-compose.prod.yml

    Hope this can be usefull

    Login or Signup to reply.
  2. As stated here:

    Environment Files Order: The environment files in the Docker Compose are processed from the top down. For example, for the same variable specified in the file ./common.env and assigned a different value in the file ./apps/db.env, the value from ./apps/db.env stands as this file is listed below (after) the ./common.env file.

    So the variable defined for a second time in .env.prod-db takes president over .env

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search