以下是一个基于Centos7安装docker的脚本
logging.sh
[root@bogon ~]# cat logging.sh
#!/bin/bash
NC='\033[0m'
RED='\033[31m'
GREEN='\033[32m'
YELLOW='\033[33m'
BLUE='\033[34m'
log::err() {
printf "[$(date +'%Y-%m-%dT%H:%M:%S.%2N%z')][${RED}ERROR${NC}] %b\n" "$@"
}
log::info() {
printf "[${GREEN}$(date +'%Y-%m-%dT%H:%M:%S.%2N%z')][${YELLOW}INFO]${NC} %b\n" "$*"
}
log::warning() {
printf "[$(date +'%Y-%m-%dT%H:%M:%S.%2N%z')][${YELLOW}WARNING${NC}] \033[0m%b\n" "$@"
}
test.sh
#!/bin/bash
source logging.sh
# 检查包是否安装
#if rpm -qa | grep -q telnet; then
# log::warning "${RED} [-] telnet 安装包已存在 "
#else
# yum -y install telnet > /dev/null 2>&1
# log::info "${GREEN} [+] 安装基础依赖包 yum -y install telnet"
#fi
# 关闭防火墙
systemctl stop firewalld
log::info "${GREEN} [+] 关闭防火墙:systemctl stop firewalld "
systemctl disable firewalld > /dev/null 2>&1
log::info "${GREEN} [+] 禁止开机自启:systemctl disable firewalld "
# 安装docker
log::info "${GREEN} [+] 安装docker基础组件:yum -y install -y yum-utils device-mapper-persistent-data lvm2 --skip-broken"
yum -y install -y yum-utils device-mapper-persistent-data lvm2 --skip-broken
log::info "${GREEN} [+] 添加仓库:yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo"
yum-config-manager \
--add-repo \
https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
log::info "${GREEN} [+] 替换仓库:sed -i 's/download.docker.com/mirrors.aliyun.com docker-ce/g' /etc/yum.repos.d/docker-ce.repo"
sed -i 's/download.docker.com/mirrors.aliyun.com\/docker-ce/g' /etc/yum.repos.d/docker-ce.repo
log::info "${GREEN} [+] 创建缓存:yum makecache fast"
yum makecache fast
log::info "${GREEN} [+] 安装docker-ce引擎:yum install -y docker-c"
yum install -y docker-ce
log::info "${GREEN} [+] 创建docker目录:sudo mkdir -p /etc/docker"
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://uj9wvi24.mirror.aliyuncs.com"]
}
EOF
log::info "${GREEN} [+] 重新加载docker:systemctl daemon-rerload"
systemctl daemon-reload
log::info "${GREEN} [+] 重新启动docker:systemctl restart docker"
systemctl restart docker
docker ps > /dev/null 2>&1
if [[ $? -eq 0 ]];then
echo -e "[+] Docker install successed!"
else
echo -e "[+] Docker install faild!"
fi
docker image ls | grep nginx > /dev/null 2>&1
if [[ $? -ne 0 ]];then
log::err "${RED} [-] nginx镜像不存在${NC}"
fi
效果
正常输出 【info】 标记绿色
异常问题 【err】 标记红色
这样看起来更直白