简体   繁体   English

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

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

I have a docker-compose file which looks like:我有一个 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"

It works as expected on running docker-compose up .它在运行docker-compose up按预期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.但是,如果我从 yml 中删除env_file选项并将其传递到 CLI - docker-compose --env-file .env up ,则它不会从文件中选择环境变量的值。

This happens because the --env-file in CLI and env_file in service section are not exactly the same thing.发生这种情况是因为 CLI 中的--env-file和 service 部分中的env_file并不完全相同。

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).前者实际上是 compose 将用来替换 docker-compose.yml 中的变量的文件的规范(默认值为 .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 .阅读这个这个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM