美文网首页
5. Docker-单机部署-多系统

5. Docker-单机部署-多系统

作者: 随便写写咯 | 来源:发表于2021-01-14 00:06 被阅读0次
一键安装docker脚本, 适用于Centos7、Centos8、Ubuntu Server 18.04
#!/bin/bash

#注意:使用root或sudo用户执行

set -e

#获取系统版本
os=$(awk -F'[=" ]' '/^NAME=/{print $3}' /etc/os-release)
version_id=$(awk -F'[="]' '/^VERSION_ID=/{print $3}' /etc/os-release)

#配置国内安装源
sources_list() {
    sudo mv /etc/apt/sources.list{,.bak}
    cat > /etc/apt/sources.list <<-EOF
    # 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
    deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
    #deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
    deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
    #deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
    deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
    #deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
    deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
    #deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
    # 预发布软件源,不建议启用
    #deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
    #deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
    EOF
    sudo apt update
}

#配置镜像加速
registry-mirror() {
    [ -d /etc/docker ] || mkdir /etc/docker
    cat > /etc/docker/daemon.json <<-EOF
    {
      "registry-mirrors" : [
        "http://registry.docker-cn.com",
        "http://docker.mirrors.ustc.edu.cn",
        "http://hub-mirror.c.163.com"
      ],
      "insecure-registries" : [
        "registry.docker-cn.com",
        "docker.mirrors.ustc.edu.cn"
      ],
      "debug" : true,
      "experimental" : true
    }
    EOF
    systemctl daemon-reload
    systemctl start docker
    systemctl enable docker
}

docker_oldversion() {
    if [ "$os" = "CentOS" -a "$version_id" = "7" ];then
        yum install -y yum-utils device-mapper-persistent-data lvm2
        yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
        centos7_docker_oldver=$(yum list docker-ce --showduplicates | awk -F'[ :]+' '/19.03/{print $3}' | tail -1)
        yum install -y docker-ce-$centos7_docker_oldver
        registry-mirror
        systemctl restart docker.service
    elif [ "$os" = "CentOS" -a "$version_id" = "8" ];then
        yum install -y yum-utils device-mapper-persistent-data lvm2
        yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
        yum install -y https://download.docker.com/linux/centos/8/x86_64/stable/Packages/containerd.io-1.4.3-3.1.el8.x86_64.rpm
        centos8_docker_oldver=$(yum list docker-ce --showduplicates | awk -F'[ :]+' '/19.03/{print $3}' | tail -1)
        yum install -y docker-ce-$centos8_docker_oldver
        registry-mirror
        systemctl restart docker.service
    elif [ "$os" = "Ubuntu" -a "$version_id" = "18.04" ];then
        sources_list
        sudo rm -f /var/lib/dpkg/lock-frontend
        sudo rm -f /var/lib/dpkg/lock
        sudo rm -f /var/cache/apt/archives/lock
        sudo rm -f /var/lib/dpkg/updates/*
        sudo apt install -y apt-transport-https ca-certificates curl software-properties-common
        curl -fsSL https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
        sudo add-apt-repository "deb [arch=amd64] https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
        sudo apt update
        ubuntu18_docker_oldver=$(sudo apt-cache madison docker-ce | awk -F '[ |]+' '/19.03/{print $3}' | head -1)
        sudo apt install -y docker-ce=$ubuntu18_docker_oldver docker-ce-cli=$ubuntu18_docker_oldver
        registry-mirror
        sudo systemctl restart docker.service
    fi
}

docker_newversion() {
    if [ "$os" = "CentOS" -a "$version_id" = "7" ];then
        yum install -y yum-utils device-mapper-persistent-data lvm2
        yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
        centos7_docker_newver=$(yum list docker-ce --showduplicates | awk -F'[ :]+' 'END{print $3}')
        yum install -y docker-ce-$centos7_docker_newver
        registry-mirror
        systemctl restart docker.service
    elif [ "$os" = "CentOS" -a "$version_id" = "8" ];then
        yum install -y yum-utils device-mapper-persistent-data lvm2
        yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
        yum install -y https://download.docker.com/linux/centos/8/x86_64/stable/Packages/containerd.io-1.4.3-3.1.el8.x86_64.rpm
        centos8_docker_newver=$(yum list docker-ce --showduplicates | awk -F'[ :]+' 'END{print $3}')
        yum install -y docker-ce-$centos8_docker_newver
        registry-mirror
        systemctl restart docker.service
    elif [ "$os" = "Ubuntu" -a "$version_id" = "18.04" ];then
        sources_list
        sudo rm -f /var/lib/dpkg/lock-frontend
        sudo rm -f /var/lib/dpkg/lock
        sudo rm -f /var/cache/apt/archives/lock
        sudo rm -f /var/lib/dpkg/updates/*
        sudo apt install -y apt-transport-https ca-certificates curl software-properties-common
        curl -fsSL https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
        sudo add-apt-repository "deb [arch=amd64] https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
        sudo apt update
        ubuntu18_docker_newver=$(sudo apt-cache madison docker-ce | awk -F '[ |]+' 'NR==1{print $3}')
        sudo apt install -y docker-ce=$ubuntu18_docker_newver docker-ce-cli=$ubuntu18_docker_newver
        registry-mirror
        sudo systemctl restart docker.service
    fi
}

#安装指定版本docker
read -ep "请输入要安装的docker版本:(1.docker 19.03,2.docker最新版) " docker_version
case $docker_version in 
    1)
        docker_oldversion
        ;;
    2)
        docker_newversion
        ;;
    *)
        echo "输入错误,只能输入1或2"
        ;;
esac

相关文章

网友评论

      本文标题:5. Docker-单机部署-多系统

      本文链接:https://www.haomeiwen.com/subject/dkkmaktx.html