容器管理-Docker

docker的组成

  • Docker主机(host):一个物理机或虚拟机,用于运行Docker服务进程和容器,也称为宿主机,node节点
  • Docker服务端(Server):Docker守护进程,运行docker容器
  • Docker客户端(Client):客户端使用docker命令或其他工具调用docker API
  • Docker仓库(Registry):保存镜像的仓库,可以搭建私有仓库harbor
  • Docker镜像(Images):镜像可以理解为创建实例使用的模板,相当于RPM或DEB包
  • Docker容器(Container):容器是从镜像生成对外提供服务的一个或一组服务,相当于将RPM包中的程序运行起来

docker优缺点

  优点:

  • 快速部署:短时间内可以部署成百上千个应用,更快速交付上线
  • 高效虚拟化:不需要额外的hypervisor支持,基于Linux内核实现应用虚拟化,相比虚拟机大幅提高性能和效率
  • 节省开支:提高服务器利用率,降低IT支出
  • 简化配置:将运行环境打包保存至容器,使用时直接启动即可
  • 环境统一:将开发、测试、生产的应用运行环境进行标准化和统一,减少环境不一样带来的各种问题
  • 快速迁移和扩展:可实现跨平台运行在物理机、虚拟机、公有云等环境,良好的兼容性可以方便将应用从A宿主机迁移到B宿主机,甚至是A平台迁移到B平台
  • 更好的实现面向服务的架构,推荐一个容器只运行一个应用,实现分布的应用模型,可以方便的进行横向扩展,复合开发中高内聚,低耦合的要求,减少不同服务之间的互相影响

  缺点:

  • 隔离性:多个容器共用宿主机的内核,各应用之间的隔离不如虚拟机彻底

docker安装

Ubuntu安装和删除docker

  Ubuntu 安装docker

#安装必要的一些系统工具
root@ubuntu1804:~# sudo apt update
root@ubuntu1804:~# sudo apt -y install apt-transport-https ca-certificates curl software-properties-common

#安装GPG证书
root@ubuntu1804:~# curl -fsSL https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
OK

#写入软件源信息
root@ubuntu1804:~# sudo add-apt-repository "deb [arch=amd64] https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu $(lsb_release -cs) stable"

#更新并安装Docker-CE
root@ubuntu1804:~# apt -y update
#安装指定版本的Docker-CE
查看Docker-CE的版本:
root@ubuntu1804:~# apt-cache madison docker-ce
 docker-ce | 5:20.10.12~3-0~ubuntu-bionic | https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu bionic/stable amd64 Packages
 docker-ce | 5:20.10.11~3-0~ubuntu-bionic | https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu bionic/stable amd64 Packages
 docker-ce | 5:20.10.10~3-0~ubuntu-bionic | https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu bionic/stable amd64 Packages
 docker-ce | 5:20.10.9~3-0~ubuntu-bionic | https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu bionic/stable amd64 Packages
 docker-ce | 5:20.10.8~3-0~ubuntu-bionic | https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu bionic/stable amd64 Packages
 docker-ce | 5:20.10.7~3-0~ubuntu-bionic | https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu bionic/stable amd64 Packages
 docker-ce | 5:20.10.6~3-0~ubuntu-bionic | https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu bionic/stable amd64 Packages
 docker-ce | 5:20.10.5~3-0~ubuntu-bionic | https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu bionic/stable amd64 Packages
 docker-ce | 5:20.10.4~3-0~ubuntu-bionic | https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu bionic/stable amd64 Packages
 docker-ce | 5:20.10.3~3-0~ubuntu-bionic | https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu bionic/stable amd64 Packages
 docker-ce | 5:20.10.2~3-0~ubuntu-bionic | https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu bionic/stable amd64 Packages
 docker-ce | 5:20.10.1~3-0~ubuntu-bionic | https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu bionic/stable amd64 Packages
 docker-ce | 5:20.10.0~3-0~ubuntu-bionic | https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu bionic/stable amd64 Packages
 docker-ce | 5:19.03.15~3-0~ubuntu-bionic | https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu bionic/stable amd64 Packages
 docker-ce | 5:19.03.14~3-0~ubuntu-bionic | https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu bionic/stable amd64 Packages
 docker-ce | 5:19.03.13~3-0~ubuntu-bionic | https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu bionic/stable amd64 Packages
 docker-ce | 5:19.03.12~3-0~ubuntu-bionic | https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu bionic/stable amd64 Packages
 docker-ce | 5:19.03.11~3-0~ubuntu-bionic | https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu bionic/stable amd64 Packages
 docker-ce | 5:19.03.10~3-0~ubuntu-bionic | https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu bionic/stable amd64 Packages
 docker-ce | 5:19.03.9~3-0~ubuntu-bionic | https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu bionic/stable amd64 Packages
 docker-ce | 5:19.03.8~3-0~ubuntu-bionic | https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu bionic/stable amd64 Packages
 docker-ce | 5:19.03.7~3-0~ubuntu-bionic | https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu bionic/stable amd64 Packages
 docker-ce | 5:19.03.6~3-0~ubuntu-bionic | https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu bionic/stable amd64 Packages
 docker-ce | 5:19.03.5~3-0~ubuntu-bionic | https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu bionic/stable amd64 Packages
 docker-ce | 5:19.03.4~3-0~ubuntu-bionic | https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu bionic/stable amd64 Packages
 docker-ce | 5:19.03.3~3-0~ubuntu-bionic | https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu bionic/stable amd64 Packages
 docker-ce | 5:19.03.2~3-0~ubuntu-bionic | https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu bionic/stable amd64 Packages
 docker-ce | 5:19.03.1~3-0~ubuntu-bionic | https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu bionic/stable amd64 Packages
 docker-ce | 5:19.03.0~3-0~ubuntu-bionic | https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu bionic/stable amd64 Packages
 docker-ce | 5:18.09.9~3-0~ubuntu-bionic | https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu bionic/stable amd64 Packages
 docker-ce | 5:18.09.8~3-0~ubuntu-bionic | https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu bionic/stable amd64 Packages
 docker-ce | 5:18.09.7~3-0~ubuntu-bionic | https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu bionic/stable amd64 Packages
 docker-ce | 5:18.09.6~3-0~ubuntu-bionic | https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu bionic/stable amd64 Packages
 docker-ce | 5:18.09.5~3-0~ubuntu-bionic | https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu bionic/stable amd64 Packages
 docker-ce | 5:18.09.4~3-0~ubuntu-bionic | https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu bionic/stable amd64 Packages
 docker-ce | 5:18.09.3~3-0~ubuntu-bionic | https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu bionic/stable amd64 Packages
 docker-ce | 5:18.09.2~3-0~ubuntu-bionic | https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu bionic/stable amd64 Packages
 docker-ce | 5:18.09.1~3-0~ubuntu-bionic | https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu bionic/stable amd64 Packages
 docker-ce | 5:18.09.0~3-0~ubuntu-bionic | https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu bionic/stable amd64 Packages
 docker-ce | 18.06.3~ce~3-0~ubuntu | https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu bionic/stable amd64 Packages
 docker-ce | 18.06.2~ce~3-0~ubuntu | https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu bionic/stable amd64 Packages
 docker-ce | 18.06.1~ce~3-0~ubuntu | https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu bionic/stable amd64 Packages
 docker-ce | 18.06.0~ce~3-0~ubuntu | https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu bionic/stable amd64 Packages
 docker-ce | 18.03.1~ce~3-0~ubuntu | https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu bionic/stable amd64 Packages
安装指定版本的Docker-CE:
root@ubuntu1804:~# apt -y install docker-ce=5:19.03.14~3-0~ubuntu-bionic docker-ce-cli=5:19.03.14~3-0~ubuntu-bionic
#使用阿里做镜像加速
mkdir -p /etc/docker
tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://si7y70hh.mirror.aliyuncs.com"]
}
EOF
systemctl daemon-reload
systemctl restart docker

  Ubuntu一键安装docker脚本

#!/bin/bash
# 
#*************************************************************************************
#Author:                      wanghaomiao
#QQ:                          1074035975
#Date:                        2022-01-17
#FileName                     install_docker_for_ubuntu1804.sh
#URL:                         www.wanghaomiao.com.cn
#Description:                 The test script
#Copyreght (C):               2022 All rightsreserved
#*************************************************************************************
COLOR="echo -e \\033[1;31m"
END="\033[m"
DOCKER_VERSION="5:19.03.14~3-0~ubuntu-bionic"

install_docker(){
docker version &>/dev/null && ${COLOR}"Docker已安装,退出"${END} && exit
sudo apt-get update || { ${COLOR}更新包索引失败${END}; exit 1; }
sudo apt-get -y install apt-transport-https ca-certificates curl software-properties-common || { ${COLOR}安装相关包失败${END}; exit 2; }
curl -fsSL https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu $(lsb_release -cs) stable"

apt update
${COLOR}"Docker有以下版本"${END}
apt-cache madison docker-ce
${COLOR}"5秒后即将安装: docker-"${DOCKER_VERSION}" 版本....."${END}
sleep 3

apt -y install docker-ce=${DOCKER_VERSION} docker-ce-cli=${DOCKER_VERSION}

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

  删除docker

root@ubuntu1804:~# apt purge docker-ce
root@ubuntu1804:~# rm -rf /var/lib/docker/

  查看docker相关文件:

root@ubuntu1804:~# dpkg -L docker-ce
/.
/etc
/etc/default
/etc/default/docker
/etc/init
/etc/init/docker.conf
/etc/init.d
/etc/init.d/docker
/lib
/lib/systemd
/lib/systemd/system
/lib/systemd/system/docker.service
/lib/systemd/system/docker.socket
/usr
/usr/bin
/usr/bin/docker-init
/usr/bin/docker-proxy
/usr/bin/dockerd
/usr/share
/usr/share/doc
/usr/share/doc/docker-ce
/usr/share/doc/docker-ce/README.md
/usr/share/doc/docker-ce/changelog.Debian.gz

CentOS7安装和删除docker

  安装docker脚本

[root@CentOS7 ~]# cat install_docker_for_centos7.sh 
#!/bin/bash
#
#*************************************************************************************
#Author:                      wanghaomiao
#QQ:                          1074035975
#Date:                        2022-01-17
#FileName                     install_docker_for_centos7.sh
#URL:                         www.wanghaomiao.com.cn
#Description:                 The test script
#Copyreght (C):               2022 All rightsreserved
#*************************************************************************************
COLOR="echo -e \\033[1;31m"
END="\033[m"
VERSION="19.03.5-3.el7"

wget -P /etc/yum.repos.d/ https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo  || { ${COLOR}"互联网连接失败,请检查网络配置!"${END};exit; }

yum clean all
yum -y install docker-ce-$VERSION docker-ce-cli-$VERSION || { ${COLOR}"Base,Extras的yum源失败,请检查yum源配置"${END};exit; }
mkdir -p /etc/docker
cat > /etc/docker/daemon.json <<EOF
{
 "registry-mirrors": ["https://7phikgwg.mirror.aliyuncs.com"]
 }
EOF

systemctl enable --now docker
docker version && ${COLOR}"Docker安装成功"${END} || ${COLOR}"Docker安装失败"${END}

  删除docker

[root@CentOS7 ~]# yum remove docker-ce -y
[root@CentOS7 ~]# rm -rf /var/lib/docker

docker一键安装脚本

内容转载至:https://www.cnblogs.com/neteagles/p/14257143.html

#docker一键安装版本
[root@centos7 ~]# cat install_docker-v2.sh
#!/bin/bash
#
#******************************************************************************
#Author:        zhanghui
#QQ:            19661891
#Date:          2021-04-05
#FileName:      install_docker-v2.sh
#URL:           www.cnblogs.com/neteagles
#Description:   install_docker for centos 7/8 & ubuntu 18.04/20.04
#Copyright (C): 2021 All rights reserved
#******************************************************************************
COLOR="echo -e \\033[01;31m"
END='\033[0m'

os(){
    if grep -Eqi "CentOS" /etc/issue || grep -Eq "CentOS" /etc/*-release;then
        rpm -q redhat-lsb-core &> /dev/null || { ${COLOR}"安装lsb_release工具"${END};yum -y install  redhat-lsb-core &> /dev/null; }
    fi
    OS_ID=`lsb_release -is`
    OS_RELEASE_VERSION_VERSION=`lsb_release -rs |awk -F'.' '{print $1}'`
    OS_CODENAME=`lsb_release -cs`
}

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

    ${COLOR}"开始安装DOCKER依赖包"${END}
    apt update &> /dev/null
    apt -y install apt-transport-https ca-certificates curl software-properties-common &> /dev/null
    curl -fsSL https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu/gpg | sudo apt-key add - &> /dev/null
    add-apt-repository  "deb [arch=amd64] https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu  ${OS_CODENAME} 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="19.03.15-3.el${OS_RELEASE_VERSION}"
    cat > /etc/yum.repos.d/docker.repo <<-EOF
[docker]
name=docker
gpgcheck=0
baseurl=https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/${OS_RELEASE_VERSION}/x86_64/stable/
EOF
    yum clean all &> /dev/null

    ${COLOR}"Docker有以下版本"${END}
    yum list docker-ce.x86_64 --showduplicates  |grep docker-ce |sort -nr
    ${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}"Base,Extras的yum源失败,请检查yum配置"${END};exit; }
}

aliyun_jxjsq(){
    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 imasges -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 -i.bak 's/GRUB_CMDLINE_LINUX=.*/GRUB_CMDLINE_LINUX=" net.ifnames=0 cgroup_enable=memory swapaccount=1"/' /etc/default/grub
    chmod u-w /etc/default/grub ;update-grub
    ${COLOR}"10秒后,机器会自动重启"${END}
    sleep 10
    reboot
}

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

main


#docker菜单选择版本
[root@centos7 ~]# cat install_docker_menu_v2.sh
#!/bin/bash
#
#******************************************************************************
#Author:        zhanghui
#QQ:            19661891
#Date:          2021-01-08
#FileName:      install_docker_menu_v2.sh
#URL:           www.cnblogs.com/neteagles
#Description:   install_docker_menu for centos 7/8 & ubuntu 18.04/20.04
#Copyright (C): 2021 All rights reserved
#******************************************************************************
COLOR="echo -e \\033[01;31m"
END='\033[0m'

os(){
    if grep -Eqi "CentOS" /etc/issue || grep -Eq "CentOS" /etc/*-release;then
        rpm -q redhat-lsb-core &> /dev/null || { ${COLOR}"安装lsb_release工具"${END};yum -y install  redhat-lsb-core &> /dev/null; }
    fi
    OS_RELEASE_VERSION=`lsb_release -rs |awk -F'.' '{print $1}'`
    OS_CODENAME=`lsb_release -cs`
}

ubuntu_install_docker(){
    dpkg -s docker-ce &>/dev/null && ${COLOR}"Docker已安装,退出"${END} && exit

    ${COLOR}"开始安装DOCKER依赖包"${END}
    apt update &> /dev/null
    apt -y install apt-transport-https ca-certificates curl software-properties-common &> /dev/null
    curl -fsSL https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu/gpg | sudo apt-key add - &> /dev/null
    add-apt-repository  "deb [arch=amd64] https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu  ${OS_CODENAME} 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
}

ubuntu_docker_version_2010(){
    DOCKER_VERSION="5:20.10.5~3-0~ubuntu-${OS_CODENAME}"
}

ubuntu_docker_version_1903(){
    DOCKER_VERSION="5:19.03.15~3-0~ubuntu-${OS_CODENAME}"
}

ubuntu_docker_version_1809(){
    DOCKER_VERSION="5:18.09.9~3-0~ubuntu-${OS_CODENAME}"
}

centos_install_docker(){
    rpm -q docker-ce &> /dev/null && ${COLOR}"Docker已安装,退出"${END} && exit
    cat > /etc/yum.repos.d/docker.repo <<-EOF
[docker]
name=docker
gpgcheck=0
baseurl=https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/${OS_RELEASE_VERSION}/x86_64/stable/
EOF
    yum clean all &> /dev/null

    ${COLOR}"Docker有以下版本"${END}
    yum list docker-ce.x86_64 --showduplicates |grep docker-ce |sort -nr
    ${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}"Base,Extras的yum源失败,请检查yum配置"${END};exit; }
}

centos_docker_version_2010(){
    DOCKER_VERSION="20.10.5-3.el${OS_RELEASE_VERSION}"
}

centos_docker_version_1903(){
    DOCKER_VERSION="19.03.15-3.el${OS_RELEASE_VERSION}"
}

aliyun_jxjsq(){
    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 &> /dev/null
    docker version &&  ${COLOR}"Docker 安装成功"${END} || ${COLOR}"Docker 安装失败"${END}
}

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

PS3="请选择相应的Docker版本(1-6):" 
MENU="
Ubuntu_Docker_5:20.10.5~3-0版本
Ubuntu_Docker_5:19.03.15~3-0版本
Ubuntu_Docker_5:18.09.9~3-0版本
CentOS_Docker_20.10.5-3版本
CentOS_Docker_19.03.15-3版本
退出
"

os

select menu in $MENU;do
    case $REPLY in
    1)
        ubuntu_docker_version_2010
        ubuntu_install_docker
        aliyun_jxjsq
        set_alias
        break
        ;;
    2)
        ubuntu_docker_version_1903
        ubuntu_install_docker
        aliyun_jxjsq
        set_alias
        break
        ;;
    3)
        ubuntu_docker_version_1809
        ubuntu_install_docker
        aliyun_jxjsq
        set_alias    
        break
        ;;
    4)
        centos_docker_version_2010
        centos_install_docker
        aliyun_jxjsq
        set_alias
        break
        ;;
    5)
        centos_docker_version_1903
        centos_install_docker
        aliyun_jxjsq
        set_alias
        break
        ;;
    6)
        break
        ;;
    *)
        ${COLOR}"输入错误,请输入正确的数字(1-6)!"${END}
        ;;
    esac
done


#docker手动输入版本
[root@centos7 ~]# cat install_docker_input_v2.sh
#!/bin/bash
#
#******************************************************************************
#Author:        zhanghui
#QQ:            19661891
#Date:          2021-01-09
#FileName:      install_docker_input_v2.sh
#URL:           www.cnblogs.com/neteagles
#Description:   install_docker_input for centos 7/8 & ubuntu 18.04/20.04
#Copyright (C): 2021 All rights reserved
#******************************************************************************
COLOR="echo -e \\033[01;31m"
END='\033[0m'

os(){
    if grep -Eqi "CentOS" /etc/issue || grep -Eq "CentOS" /etc/*-release;then
        rpm -q redhat-lsb-core &> /dev/null || { ${COLOR}"安装lsb_release工具"${END};yum -y install  redhat-lsb-core &> /dev/null; }
    fi
    OS_ID=`lsb_release -is`
    OS_RELEASE_VERSION=`lsb_release -rs |awk -F'.' '{print $1}'`
    OS_CODENAME=`lsb_release -cs`
}

ubuntu_install_docker(){
    dpkg -s docker-ce &>/dev/null && ${COLOR}"Docker已安装,退出"${END} && exit

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

    ${COLOR}"Docker有以下版本"${END}
    apt-cache madison docker-ce
    read -p "请输入要安装Docker版本(例如:5:19.03.14~3-0~ubuntu-bionic):" DOCKER_VERSION
    ${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
    cat > /etc/yum.repos.d/docker.repo <<-EOF
[docker]
name=docker
gpgcheck=0
baseurl=https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/$os_version/x86_64/stable/
EOF
    yum clean all &>/dev/null

    ${COLOR}"Docker有以下版本"${END}
    yum list docker-ce.x86_64 --showduplicates |grep docker-ce |sort -nr
    read -p "请输入要安装Docker版本(例如:19.03.14-3.el8):" DOCKER_VERSION
    ${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}"Base,Extras的yum源失败,请检查yum配置"${END};exit; }
}

aliyun_jxjsq(){
    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 &>/dev/null
    docker version &&  ${COLOR}"Docker 安装成功"${END} || ${COLOR}"Docker 安装失败"${END}
}

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

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

main

 

上一篇:IDC:云计算二次浪潮来袭!如何选择?


下一篇:NET字体信息?