#!/bin/bash if [ "$UID" -ne 0 ];then echo -e "\033[33mWarn: must be root to run this script.\033[0m" exit 1 fi if [ `echo $#` != '1' ];then echo -e "\033[33mWarn: use $0 ['docker_hasten_url']\033[0m" exit 1 fi DOCKER_HASTEN_URL=$1 function check_stage(){ local stage_name=$1 if [ `echo $?` == '0' ];then echo -e "\033[32m${stage_name} stage ---> exec success.\033[0m" else echo -e "\033[31m${stage_name} stage ---> exec fail.\033[0m" exit 1 fi } function update_docker_repo01(){ echo -e "\033[32mLog ---> exec init docker yum repo and install docker-ce......\033[0m" echo " " yum install -y yum-utils device-mapper-persistent-data lvm2 wget >& /dev/null && \ yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo >& /dev/null && \ sed -i 's+download.docker.com+mirrors.aliyun.com/docker-ce+' /etc/yum.repos.d/docker-ce.repo && \ yum makecache fast >& /dev/null && \ yum -y install docker-ce >& /dev/null || yum -y install docker-ce >& /dev/null || echo -e "\033[31mError ---> 01 yum deployment failure due to configuration or network reasons.\033[0m" } function update_docker_repo02(){ echo " " echo -e "\033[32mLog ---> exec init docker yum repo and install docker-ce......\033[0m" sed -i "/keepcache=0/i\sslverify=0" /etc/yum.conf && yum clean all \ yum install -y yum-utils device-mapper-persistent-data lvm2 wget >& /dev/null && \ wget -O /etc/yum.repos.d/docker-ce.repo https://download.docker.com/linux/centos/docker-ce.repo --no-check-certificate >& /dev/null && \ sed -i 's+download.docker.com+mirrors.tuna.tsinghua.edu.cn/docker-ce+' /etc/yum.repos.d/docker-ce.repo && \ yum makecache fast >& /dev/null && \ yum -y install docker-ce >& /dev/null || yum -y install docker-ce >& /dev/null || echo -e "\033[31mError ---> 02 yum deployment failure due to configuration or network reasons.\033[0m" } function update_docker_repo(){ update_docker_repo01 || update_docker_repo02 } function docker_start(){ echo -e "\033[32mLog ---> exec docker-ce start and enable......\033[0m" echo " " systemctl start docker && systemctl enable docker >& /dev/null check_stage 'docker_start' } function config(){ local docker_hasten_url=$1 echo -e "\033[32mLog ---> exec init docker config......\033[0m" echo " " cat > /etc/docker/daemon.json << EOF { "registry-mirrors": ["${docker_hasten_url}"], "exec-opts": ["native.cgroupdriver=systemd"] } EOF systemctl daemon-reload && systemctl restart docker >& /dev/null check_stage 'config' } function check_docker(){ local STR=$(systemctl status docker | awk 'NR==3'| awk -F "[()]" '{print $2}') rpm -qa|grep "docker-ce" >& /dev/null if [ `echo $?` -eq "0" ];then echo -e "\033[32mLog ---> docker install success!\033[0m" else echo -e "\033[31mErroe ---> docker install fail!\033[0m" exit 1 fi if [ $STR = running ];then echo " " echo -e "\033[32mLog ---> docker start success!\033[0m" else echo -e "\033[31mError ---> docker start fail!\033[0m" exit 1 fi } function main(){ update_docker_repo && sleep 1s docker_start && sleep 1s config $DOCKER_HASTEN_URL check_docker } main