DNMP中通过SHELL实现docker-compose.yml创建指定容器

  本文采用开源项目DNMP作为Docker+Nginx+MySQL+PHP容器创建依赖工具,通过修改.env重点环境变量以及docker-compose.yml,来实现某些扩展项目的添加以及某些容器的创建。docker-compose.yml默认开启容器nginx,php,mysql。因docker-compose.yml原有容器例如,phpmyadmin、elasticsearch、mongodb、supervisor等都通过‘#’字符已注释,因此可以指定添加的容器名称来实现指定容器的添加。

#!/bin/bash

Add_docker_name="phpmyadmin"
#openresty,openresty,redis,memcached,rabbitmq,phpmyadmin, \
#phpredisadmin,phpredisadmin,mongodb,adminmongo,elasticsearch, \
#kibana,logstash,node,supervisor

init(){
    yum intall -y git docker docker-compose
}

env_extension(){
  #.env修改,添加php扩展插件zip
  grep -E "^PHP_EXTENSIONS=pdo_mysql,mysqli,mbstring,gd,curl,opcache$" .env
  if [ $? -eq 0 ];then
    echo "change .env"
    sed -i 's/PHP_EXTENSIONS=pdo_mysql,mysqli,mbstring,gd,curl,opcache/PHP_EXTENSIONS=pdo_mysql,mysqli,mbstring,gd,curl,opcache,zip/' .env
  fi
}

docker_compose_yml(){
  grep -E "${Add_docker_name}:$" docker-compose.yml |grep -E "^.*#" >/dev/null 2>&1
  if [ $? -eq 0 ];then
    echo "change docker-compose.yml"
    #1.获取包含phpmyadmin:$的行号
    first_line=`grep -inE "${Add_docker_name}:$" docker-compose.yml|awk '{print $1}'| grep -E -o '\w+'`
    #echo "first_line=${first_line}"

    #2.获取自phpmyadmin_line开始后的第一个空行序号
    end_line_num=`tail -n +${first_line} docker-compose.yml |grep -nE "^$"|awk 'NR==1 {print $1}'|grep -E -o '\w+'`
    let end_line=${end_line_num}+${first_line}-2
    #echo "end_line=${end_line}"

    #3.文件中${phpmyadmin_line_num}区间段带#号开头的文本取消注释
    sed -i "${first_line},${end_line}s/^.*#//" docker-compose.yml
  fi
}

dnmp_conf(){
  env_extension
  docker_compose_yml
}

dnmp_download(){
  git clone https://github.com/yeszao/dnmp.git
  cd dnmp
  cp env.sample .env                                # 复制环境变量文件
  cp docker-compose.sample.yml docker-compose.yml
}

dnmp_start(){
  //添加zip 和phpMyadmin
  docker-compose up &
}

#init
#dnmp_download
#dnmp_conf
#dnmp_start

发表评论

邮箱地址不会被公开。 必填项已用*标注