陈佳浩
陈佳浩
发布于 2024-06-26 / 31 阅读
0
0

docker安装

#!/bin/bash
#
#********************************************************************
#Author:               	chenjiahao
#QQ:                    1938191576
#Date:                  2022-09-01
#FileName:           	install_docker-ce.sh
#URL:                   https://www.placjh.com
#Description:        	The deploy script
#Copyright (C):		2022 All rights reserved
#********************************************************************

UBUNTU_VERSION=5:20.10.17~3-0~ubuntu-focal
CENTOS_VERSION=20.10.17-3.el7
SPEED_UP='{
	"registry-mirrors": ["https://yct1mrfl.mirror.aliyuncs.com"]
}'

color () {
	RES_COL=60
	MOVE_TO_COL="echo -en \\033[${RES_COL}G"
	SETCOLOR_SUCCESS="echo -en \\033[1;32m"
	SETCOLOR_FAILURE="echo -en \\033[1;31m"
	SETCOLOR_WARNING="echo -en \\033[1;33m"
	SETCOLOR_NORMAL="echo -en \E[0m"
	echo -n "$1" && $MOVE_TO_COL
	echo -n "["
	if [ $2 = "success" -o $2 = "0" ] ;then
		${SETCOLOR_SUCCESS}
		echo -n $"  OK  "  
	elif [ $2 = "failure" -o $2 = "1"  ] ;then 
		${SETCOLOR_FAILURE}
		echo -n $"FAILED"
	else
		${SETCOLOR_WARNING}
		echo -n $"WARNING"
	fi
	${SETCOLOR_NORMAL}
	echo -n "]"
	echo 
}

. /etc/os-release

prepare(){
	if [ $ID = "ubuntu" ];then
		apt update &>/dev/null
		apt -y install wget apt-transport-https ca-certificates curl software-properties-common &>/dev/null
		curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add - &>/dev/null
		add-apt-repository "deb [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable" &>/dev/null
		apt -y update &>/dev/null
	elif [ $ID = "centos" ];then
		if [ $VERSION_ID = "7" ];then
			yum -y install wget &>/dev/null
			wget -O /etc/yum.repos.d/docker-ce.repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo &>/dev/null
			yum clean all &>/dev/null
		elif [ $VERSION_ID = "8" ];then
			yum -y install wget &>/dev/null
			cat >> /etc/yum.repos.d/docker.repo <<-EOF
			[docker]
			name=docker
			gpgcheck=0
			baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/8/x86_64/stable/
			EOF
		else
			color 系统版本不支持!1
		fi
	else
		color OS不支持! 1
		exit
	fi
}

install(){
	if [ $ID = "ubuntu" ];then
		echo "列出所有docker-ce的包:"
		echo -e "\E[32;1m"
		apt-cache madison docker-ce
		echo -e "\E[0m"
		echo "请在下面输入要安装的版本号,默认为$UBUNTU_VERSION"
		read -p "请输入要安装的版本号:" -t 5 VERSION
		echo -e "\n开始安装。。。"
		apt -y install docker-ce=${VERSION:-$UBUNTU_VERSION} docker-ce-cli=${VERSION:-$UBUNTU_VERSION}  &>/dev/null
	elif [ $ID = "centos" ];then
		echo "列出所有docker-ce的包:"
		echo -e "\E[32;1m"
		yum list docker-ce --showduplicates
		echo -e "\E[0m"
		echo "请在下面输入要安装的版本号,默认为$CENTOS_VERSION"
		read -p "请输入要安装的版本号:" -t 5 VERSION
		echo -e "\n开始安装。。。"
		yum -y install docker-ce-${VERSION:-$CENTOS_VERSION} docker-ce-cli-${VERSION:-$CENTOS_VERSION} &>/dev/null
		systemctl enable --now docker
	else
		color OS不支持! 1
		exit
	fi
	if [ $? -eq 0 ];then
		color 安装完成 0
	else
		color 安装失败 1
	fi
}

speed_up(){
	if ! [ -f /etc/docker/daemon.json ];then
		[ -d /etc/docker ] || mkdir -p /etc/docker
		cat > /etc/docker/daemon.json <<-EOF
		$SPEED_UP
		EOF
		systemctl daemon-reload
		systemctl restart docker
		color 已添加镜像加速 0
	fi
}

prepare
install
speed_up


评论