docker一鍵安裝指令碼(適用於centos7,centos8及ubuntu)
阿新 • • 發佈:2020-11-14
#!/bin/sh . /etc/init.d/functions $> /dev/null set -e COLOR="echo -e \\E[1;32m" COLOR1="echo -e \\E[1;31m" END="\\E[0m" #ubuntu依賴包 ubuntu_page=" apt-transport-https ca-certificates curl software-properties-common " #centos依賴包 centos_page=" yum-utils device-mapper-persistent-data lvm2 " #centos7安裝 install_centos7() { ${COLOR}"開始安裝 Docker....."${END} #安裝依賴包 for PAGE in ${centos_page};do rpm -q $PAGE &> /dev/null || yum -y -q install $PAGE done #新增源資訊 yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo && action "新增源資訊成功!" || { action "新增源資訊失敗,請檢查命令!" false ; exit; }#更新安裝docker-ce yum makecache fast &> /dev/null yum -y install docker-ce &> /dev/null #配置映象加速器 mkdir -p /etc/docker cat > /etc/docker/daemon.json <<-'EOF' { "registry-mirrors": ["https://jcbawuya.mirror.aliyuncs.com"] } EOF #過載配置檔案 systemctl daemon-reload #啟動服務 ${COLOR}"正在啟動 Docker....."${END} sleep 2 systemctl start docker && action "docker啟動成功!" || action "docker啟動失敗,請檢查配置檔案!" false } #centos8安裝 install_centos8() { ${COLOR}"開始安裝 Docker....."${END} #安裝依賴包 for PAGE in ${centos_page};do rpm -q $PAGE &> /dev/null || yum -y -q install $PAGE done yum -y install https://mirrors.aliyun.com/docker-ce/linux/centos/7/x86_64/stable/Packages/containerd.io-1.2.13-3.1.el7.x86_64.rpm &> /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 #更新安裝docker-ce yum clean all &> /dev/null yum -y install docker-ce &> /dev/null && action "docker安裝完成!" || action "docker按照失敗!請檢查網路及Yum源!" false #配置映象加速器 mkdir -p /etc/docker cat > /etc/docker/daemon.json <<-'EOF' { "registry-mirrors": ["https://jcbawuya.mirror.aliyuncs.com"] } EOF #過載配置檔案 systemctl daemon-reload #啟動服務 ${COLOR}"正在啟動 Docker....."${END} sleep 2 systemctl start docker && action "docker啟動成功!" || action "docker啟動失敗,請檢查配置檔案!" false } #ubuntu安裝 install_ubuntu() { ${COLOR}"開始安裝 Docker....."${END} #更新源及安裝依賴包 apt-get update &> /dev/null for PAGE in ${ubuntu_page};do dpkg -s $PAGE &> /dev/null || apt -y install $PAGE &> /dev/null done #安裝GPG證書 curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add - #寫入軟體源資訊 echo 'deb [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/ubuntu bionic stable' >> /etc/apt/sources.list #更新源資訊 apt-get -y update &> /dev/null #配置映象加速器 mkdir -p /etc/docker cat > /etc/docker/daemon.json <<EOF { "registry-mirrors": ["https://jcbawuya.mirror.aliyuncs.com"] } EOF #安裝docker-ce ${COLOR}"正在啟動 Docker....."${END} sleep 2 apt-get -y install docker-ce &> /dev/null && ${COLOR}"docker啟動成功!"${END} || ${COLOR1}"docker啟動失敗,請檢查配置檔案!"${END} } #系統型別 ostype1=`awk -F'"' '/^VERSION_ID/{print $2}' /etc/os-release` ostype2=`awk -F'"' '/^NAME/{print $2}' /etc/os-release` if [[ $ostype2 == "CentOS Linux" ]];then if [ $ostype1 = 8 ];then install_centos8 elif [ $ostype1 = 7 ];then install_centos7 fi elif [[ $ostype2 == "Ubuntu" ]];then install_ubuntu fi