s6.基于镜像仓库一键安装docker脚本

基于镜像仓库一键安装docker脚本

root@ubuntu1804:~# cat install_docker.sh 
#!/bin/bash
#
#**********************************************************************************************
#Author:        Raymond
#QQ:            88563128
#Date:          2021-12-07
#FileName:      install_docker.sh
#URL:           raymond.blog.csdn.net
#Description:   install_docker for centos 7/8 & ubuntu 18.04/20.04 Rocky 8
#Copyright (C): 2021 All rights reserved
#*********************************************************************************************
COLOR="echo -e \\033[01;31m"
END='\033[0m'
VERSION=20.10.11
URL='mirrors.cloud.tencent.com'

os(){
    OS_ID=`sed -rn '/^NAME=/s@.*="([[:alpha:]]+).*"$@\1@p' /etc/os-release`
    OS_RELEASE_VERSION=`sed -rn '/^VERSION_ID=/s@.*="?([0-9]+)\.?.*"?@\1@p' /etc/os-release`
}

ubuntu_install_docker(){
    dpkg -s docker-ce &>/dev/null && ${COLOR}"Docker已安装,退出"${END} && exit
    local DOCKER_VERSION="5:${VERSION}~3-0~ubuntu-$(lsb_release -cs)"

    ${COLOR}"开始安装DOCKER依赖包"${END}
    apt update &> /dev/null
    apt -y install apt-transport-https ca-certificates curl software-properties-common &> /dev/null
    curl -fsSL https://${URL}/docker-ce/linux/ubuntu/gpg | sudo apt-key add - &> /dev/null
    add-apt-repository  "deb [arch=amd64] https://${URL}/docker-ce/linux/ubuntu  $(lsb_release -cs) stable" &> /dev/null 
    apt update &> /dev/null

    ${COLOR}"Docker有以下版本"${END}
    apt-cache madison docker-ce
    ${COLOR}"10秒后即将安装:Docker-"${DOCKER_VERSION}"版本......"${END}
    ${COLOR}"如果想安装其它Docker版本,请按Ctrl+c键退出,修改版本再执行"${END}
    sleep 10

    ${COLOR}"开始安装DOCKER"${END}
    apt -y install docker-ce=${DOCKER_VERSION} docker-ce-cli=${DOCKER_VERSION} &> /dev/null
}

centos_install_docker(){
    rpm -q docker-ce &> /dev/null && ${COLOR}"Docker已安装,退出"${END} && exit
    local DOCKER_VERSION="${VERSION}-3.el${OS_RELEASE_VERSION}"
    cat > /etc/yum.repos.d/docker.repo <<-EOF
[docker]
name=docker
baseurl=https://${URL}/docker-ce/linux/centos/\$releasever/x86_64/stable/
gpgcheck=1
gpgkey=https://${URL}/docker-ce/linux/centos/gpg
EOF
    yum clean all &> /dev/null
	yum makecache &> /dev/null

    ${COLOR}"Docker有以下版本"${END}
    yum list docker-ce.x86_64 --showduplicates
    ${COLOR}"10秒后即将安装:Docker-"${DOCKER_VERSION}"版本......"${END}
    ${COLOR}"如果想安装其它Docker版本,请按Ctrl+c键退出,修改版本再执行"${END}
    sleep 10

    ${COLOR}"开始安装DOCKER"${END}
    yum -y install docker-ce-${DOCKER_VERSION} docker-ce-cli-${DOCKER_VERSION} &> /dev/null || { ${COLOR}"yum源失败,请检查yum配置"${END};exit; }
}

mirror_accelerator(){
    mkdir -p /etc/docker
    tee /etc/docker/daemon.json <<-'EOF'
{
    "registry-mirrors": ["https://hzw5xiv7.mirror.aliyuncs.com"]
}
EOF
    systemctl daemon-reload
    systemctl enable --now docker
    docker version &&  ${COLOR}"Docker 安装成功"${END} || ${COLOR}"Docker 安装失败"${END}
}

set_alias(){
    echo 'alias rmi="docker images -qa|xargs docker rmi -f"' >> ~/.bashrc
    echo 'alias rmc="docker ps -qa|xargs docker rm -f"' >> ~/.bashrc
}

set_swap_limit(){
    ${COLOR}'设置Docker的"WARNING: No swap limit support"警告'${END}
    chmod u+w /etc/default/grub
    sed -ri.bak '/^GRUB_CMDLINE_LINUX=/s@"$@ swapaccount=1"@' /etc/default/grub
    chmod u-w /etc/default/grub ;update-grub &> /dev/null
    ${COLOR}"10秒后,机器会自动重启"${END}
    sleep 10
    reboot
}

main(){
    os
    if [ ${OS_ID} == "CentOS" -o ${OS_ID} == "Rocky" ] &> /dev/null;then
        centos_install_docker
        mirror_accelerator
        set_alias
    else
        ubuntu_install_docker
        mirror_accelerator
        set_alias
        set_swap_limit
    fi
}

main
上一篇:ECS共享型s6华北5呼和浩特怎么样?


下一篇:分布式单点登录SSO的实现原理与方案详解