docker-compose env file working in yml but not with command line arguments

Viewed 667

I have a docker-compose file which looks like:

version: '3.8'
services:
    scheduler:
        image: apache/airflow
        command: scheduler
        restart: on-failure
        env_file:
            - .env
        volumes:
            - ./dags:/opt/airflow/dags
            - ./logs:/opt/airflow/logs
    webserver:
        image: apache/airflow
        entrypoint: ./scripts/entrypoint.sh
        restart: on-failure
        depends_on:
            - scheduler
        env_file:
            - .env
        volumes:
            - ./dags:/opt/airflow/dags
            - ./logs:/opt/airflow/logs
            - ./scripts:/opt/airflow/scripts
        ports:
            - "8080:8080"

It works as expected on running docker-compose up. But if I remove the env_file option from yml and pass it in CLI - docker-compose --env-file .env up, then it doesn't pick the value of environment variables from the file.

1 Answers

This happens because the --env-file in CLI and env_file in service section are not exactly the same thing.

The former is actually a specification of the file that compose will use to substitute variables inside docker-compose.yml (the default value is .env).

The latter is specifying the file from which the variables will be loaded and passed to the container of the service.

And yes, it's kinda confusing.

Reference:

Read this and this.

Related