基于Ubuntu与Centos一键安装Docker
基于Ubuntu与Centos一键安装Docker

基于Ubuntu与Centos一键安装Docker

#!/bin/bash

Repository="mirrors.tuna.tsinghua.edu.cn"

cecho() {
	echo -e "\e[$[RANDOM%7+31];1m $1 \e[0m"
}

dockerCheck() {
	if [ $(lsb_release -is) == "Ubuntu" ];then
	    dpkg -s docker-ce  &> /dev/null
	elif [ $(lsb_release -is) == "CentOS" ];then
	    rpm -q docker-ce &> /dev/null 
    else
    	cecho "This OS is not suitable for installation";exit 1;
    fi
    [ $? -eq 0 ] && { cecho "docker is alrelady installed";exit 0; }
}

Ubuntu() {
	apt update
	apt -y install apt-transport-https ca-certificates curl software-properties-common
	curl -fssl  https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
	add-apt-repository "deb [arch=amd64] https://${Repository}/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
	apt update

	cecho "docker有以下版本"
	apt-cache madison docker-ce | grep "5:" | sed -r 's/([a-z-]+)[ |]+([5:[:alnum:]~.-]+).*/\1:\2/p'
    read -p "请输入需要安装的docker版本: " VERSION
    cecho "docker-ce-$VERSION will be install after 5s"
    sleep 5
    apt -y install docker-ce=$VERSION docker-ce-cli=$VERSION && cecho "docker安装完成" || { cecho "请检查网络以及源的可用性";exit 1; }
}

Centos() {
cat > /etc/yum.repos.d/docker.repo <<EOF
[docker]
name=docker
gpgcheck=0
baseurl=https://$Repository/docker-ce/linux/centos/\$releasever/x86_64/stable/
EOF

cecho "软件仓库重构中。。。"
yum clean all
cecho "软件仓库重构完成"
cecho "docker有以下版本"
yum list --showduplicates |grep -E "docker-\ce[.]" | grep "3:" | sed -r "s/([a-z-]+).*:([[:alnum:].-]+).*/\1:\2/p"
read -p "请输入需要安装的docker版本: " VERSION
yum install -y docker-ce-${VERSION}  docker-ce-cli-${VERSION} && cecho "docker安装完成" || { cecho "请检查网络以及源的可用性";exit 1; }
}

installDocker() {
yum install -y redhat-lsb-core wget &>/dev/null
dockerCheck
cecho "开始安装docker"
[ $(lsb_release -is) == "Ubuntu" ] && Ubuntu || Centos

mkdir -p /etc/docker
	cat >/etc/docker/daemon.json <<EOF
{
	"registry-mirrors":["https://hub-mirror.c.163.com"]
}
EOF
systemctl daemon-reload
systemctl enable --now docker
}

cecho "依赖软件安装中。。。"
installDocker

一条评论

  1. Pingback:十、Docker 仓库管理 – Linux学习资料大全

发表回复

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