docker-compose.yml文件使用变量参数和定义容器的环境变量

docker-compose.yml文件使用变量参数:

jasper@jasper-S1200RP:~/test/testcompose$ cat docker-compose.yml
  version: '3.7'   
  services:     
      appshield:      
            build:        
                context: .
                args:          
                    VER: ${APPSHIELD_VER}
            image: nginx:${APPSHIELD_VER}  
     
            restart: always
            container_name: engine_appshield       
            hostname: engine_appshield       
            privileged: true       
jasper@jasper-S1200RP:~/test/testcompose$ cat .env
APPSHIELD_VER=latest

检查变量是否生效:

jasper@jasper-S1200RP:~/test/testcompose$ docker-compose config
services:
  appshield:
    build:
      args:
        VER: latest
      context: /home/jasper/test/testcompose
    container_name: engine_appshield
    hostname: engine_appshield
    image: nginx:latest
    privileged: true
    restart: always
version: '3.7'

docker-compose.yml定义容器的环境变量 env_file:

jasper@jasper-S1200RP:~/test/testcompose$ cat docker-compose.yml
  version: '3.7'   
  services:     
      appshield:      
            build:        
                context: ./
                args:          
                    VER: ${APPSHIELD_VER}
            image: nginx:${APPSHIELD_VER}       
            restart: always
            container_name: engine_appshield       
            hostname: engine_appshield       
            privileged: true
            env_file:
              - ./env/common.env

jasper@jasper-S1200RP:~/test/testcompose$ mkdir env
jasper@jasper-S1200RP:~/test/testcompose$ vim env/common.env

jasper@jasper-S1200RP:~/test/testcompose$ cat env/common.env
JAVA_HOME=/usr/local/java


检测环境变量是否生效:

jasper@jasper-S1200RP:~/test/testcompose$ docker-compose config
services:
  appshield:
    build:
      args:
        VER: latest
      context: /home/jasper/test/testcompose
    container_name: engine_appshield
    environment:
      JAVA_HOME: /usr/local/java

    hostname: engine_appshield
    image: nginx:latest
    privileged: true
    restart: always
version: '3.7'

 

 

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页