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'

 

 

你可能感兴趣的:(docker-compose)