环境变量的修改

docker-compose的文件是不支持环境变量的使用,故为了适应在不同环境中配置不同的变量值如IP地址,则需要利用envsubst这个程序。
1.创建一个与环境变量相关的docker模板,命名为base-compose.yml,内容大概如下:。

version: '3'
services:
  cadvisor:
    ## Image
    build: cadvisor
    ##image: google/cadvisor:v0.27.1

    ## Base
    privileged: false
    restart: always
    container_name: cadvisor
    hostname: ${ACADVISOR_HOSTNAME}
    command: -storage_driver=influxdb -storage_driver_db=${ACADVISOR_DB} -storage_driver_host=${INFLUXBD_SERVER_IP}:${INFLUXDB_SERVER_PORT}  

    ## Network
    #network_mode: host  
    #expose:
    ports:
      - "9090:8080"
    #depends_on:
    #link:

    ## Storage
    volumes:
      - /etc/localtime:/etc/localtime:ro
      - /etc/timezone:/etc/timezone:ro 
      - /:/rootfs:ro
      - /var/run:/var/run:rw
      - /sys:/sys:ro
      - /var/lib/docker/:/var/lib/docker:ro

    ## Env
    environment:
      - SERVICE_IGNORE=true

2.具体的环境变量如下,文件如下:

#!/bin/bash
export PRIVATE_IP=$(cat /etc/hosts | grep `hostname` |awk '{print $1}')
ret=$(curl https://ipapi.co/json| grep -v org)
ret=`echo $ret | sed 's/ip/public_ip/g'`
IP_INFO=`echo $ret | sed 's/"//g'| sed 's/{//g'| sed 's/}//g'| sed '/^$/g'| sed 's/[[:space:]]//g'`

export PUBLIC_IP=`echo $IP_INFO | awk -F ',' '{print $1}' | awk -F ':' '{print $2}'`

export MONITOR_VOLUME='/home/xxx/volume/monitor'
export KMS_VOLUME='/home/xxx/volume/kms'

export ACADVISOR_HOSTNAME=`hostname`
export ACADVISOR_DB='cadvisor'

export INFLUXBD_SERVER_IP=${PUBLIC_IP}
export INFLUXDB_SERVER_PORT='8086'

export GRAFANA_SERVER_IP=${PUBLIC_IP}
export GRAFANA_SERVER_PORT='13000'

export GRAPHITE_SERVER_IP=${PUBLIC_IP}
export GRAPHITE_SERVER_PORT='12003'

export ESEARCH_SERVER_URL='http://'${PUBLIC_IP}':9200'

3.替换指令

#!/bin/bash
source env.sh
envsubst  docker-compose.yml