Stay Hungry,Stay Foolish!

Environment variables precedence- docker compose

Environment variables precedence

https://docs.docker.com/compose/environment-variables/envvars-precedence/

 

The order of precedence (highest to lowest) is as follows:

  1. Set using docker compose run -e in the CLI
  2. Substituted from your shell
  3. Set using the environment attribute in the Compose file
  4. Use of the --env-file argument in the CLI
  5. Use of the env_file attribute in the Compose file
  6. Set using an .env file placed at base of your project directory
  7. Set in a container image in the ENV directive. Having any ARG or ENV setting in a Dockerfile evaluates only if there is no Docker Compose entry for environment, env_file or run --env.

the host’s shell:

https://vsupalov.com/override-docker-compose-dot-env/

$ NAME="ENV" docker-compose up
Recreating lab_ubuntu_1 ... done
Attaching to lab_ubuntu_1
ubuntu_1  | PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
ubuntu_1  | HOSTNAME=7ee3ce6e3965
ubuntu_1  | NAME=ENV
ubuntu_1  | HOME=/root
lab_ubuntu_1 exited with code 0

 

ARG和ENV的可用性

https://zhuanlan.zhihu.com/p/152957068

一旦引入ENV指令,ENV变量在构建过程中也可用。但与ARG不同,从最终映像开始的容器也可以访问它们。启动容器时,可以覆盖ENV值,更多信息请参见下文。

这是围绕从Dockerfile构建Docker映像并运行容器的过程中ARG和ENV可用性的简化概述。它们重叠,但是不能从容器内部使用ARG。

 

 

args override in compose file.

version: '3'

services:
  somename:
    build:
      context: ./app
      dockerfile: Dockerfile
      args:
        some_variable_name: a_value

 

posted @ 2023-07-10 00:08  lightsong  阅读(6)  评论(0编辑  收藏  举报
Life Is Short, We Need Ship To Travel