docker-compose env 文件可以在 yml 中工作,但不能使用命令行参数

nis*_*ish 3 environment-variables docker docker-compose airflow

我有一个 docker-compose 文件,如下所示:

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"
Run Code Online (Sandbox Code Playgroud)

它在运行时按预期工作docker-compose up。但是,如果我从 yml 中删除该env_file选项并将其传递到 CLI - 中docker-compose --env-file .env up,那么它不会从文件中选取环境变量的值。

Mil*_*vic 5

发生这种情况是因为--env-fileCLI 中和env_file服务中部分并不完全相同。

前者实际上是 compose 将用来替换 docker-compose.yml 中的变量的文件的规范(默认值为 .env)。

后者指定将从中加载变量并将其传递到服务容器的文件。

是的,这有点令人困惑。

参考:

阅读这个这个