docker-compose load .env from dynamic path

763 Views Asked by At

I have below structure

enter image description here

The project root .env defines the path to either ./docker/dev/.env or ./docker/prod/.env as depicted below:

environment=dev
dot_env_path=./docker/dev/.env

My docker-compose.yml contains the below service snippet:

services:
    db:
        image: mysql:5.7
        command: --default-authentication-plugin=mysql_native_password
        restart: always
        env_file: ${dot_env_path} <--notice this variable points to the actual .env
        environment:
            - MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
            - MYSQL_USER=${MYSQL_USER}
            - MYSQL_PASSWORD=${MYSQL_PASSWORD}
        ports: 
            - 3306:3306
        expose: 
            - 3306
        volumes:
            - db-tmc:/var/lib/mysql

However, when I do docker-compose build I receive below warnings; it's not detecting the ${dot_env_path} and loading it's content

enter image description here

Any idea, much appreciated?

1

There are 1 best solutions below

0
Abhishek J On

Dont use environment: if you are using the env_file option. It overrides the variables already added to the environment from the env_file: option

Use this instead.

services:
    db:
        image: mysql:5.7
        command: --default-authentication-plugin=mysql_native_password
        restart: always
        env_file: ${dot_env_path}   
        ports: 
            - 3306:3306
        expose: 
            - 3306
        volumes:
            - db-tmc:/var/lib/mysql