docker-ce 安装程序
自动判断 centos / debian / ubuntu 系统
加入切换国内源和国外源地址
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
| #!/bin/bash
echo "#############################" echo "### 安装 docker-ce ###" echo "### CentOS 7 ###" echo "### CentOS Stream 8 ###" echo "### CentOS Stream 9 ###" echo "### Debian 12 (Bookworm) ###" echo "### Debian 11 (Bullseye) ###" echo "### Ubuntu 23.10 (Mantic) ###" echo "### Ubuntu 22.04 (Jammy) ###" echo "### Ubuntu 20.04 (Focal) ###" echo "#############################"
docker_mirrors=https://mirrors.aliyun.com/docker-ce/linux
function install_docker_ce_centos() { echo "卸载旧版本" sudo yum remove -y docker \ docker-client \ docker-client-latest \ docker-common \ docker-latest \ docker-latest-logrotate \ docker-logrotate \ docker-engine \ docker-selinux
echo "安装需要的软件包" sudo yum install -y yum-utils device-mapper-persistent-data lvm2
echo "设置yum源" sudo yum-config-manager --add-repo $docker_mirrors/centos/docker-ce.repo
echo "安装docker ..." sudo yum install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
echo "启动docker-ce服务并将其加入开机自启" sudo systemctl start docker
sudo systemctl enable docker
echo "查看 docker-ce 版本 ..." docker version }
function install_docker_ce_debian() { echo "卸载以避免与 Docker Engine 版本冲突 ..." for pkg in docker.io docker-doc docker-compose podman-docker containerd runc; do sudo apt-get remove $pkg; done echo "更新apt包索引并安装包以允许apt通过 HTTPS 使用存储库" sudo apt-get -y update sudo apt-get -y install ca-certificates curl gnupg echo "添加Docker官方GPG密钥" sudo install -m 0755 -d /etc/apt/keyrings curl -fsSL $docker_mirrors/debian/gpg -o /etc/apt/keyrings/docker.asc sudo chmod a+r /etc/apt/keyrings/docker.asc echo "将存储库添加到 Apt 源" echo \ "deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.asc] $docker_mirrors/debian \ "$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \ tee /etc/apt/sources.list.d/docker.list > /dev/null
echo "更新apt包索引" sudo apt-get -y update
echo "安装 Docker 引擎、containerd 和 Docker Compose 最新版" sudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin echo "查看 docker-ce 版本 ..." docker version
}
function install_docker_ce_ubuntu() { echo "卸载以避免与 Docker Engine 版本冲突 ..." for pkg in docker.io docker-doc docker-compose docker-compose-v2 podman-docker containerd runc; do sudo apt-get remove $pkg; done echo "更新apt包索引并安装包以允许apt通过 HTTPS 使用存储库" sudo apt-get -y update sudo apt-get -y install ca-certificates curl echo "添加Docker官方GPG密钥" sudo install -m 0755 -d /etc/apt/keyrings curl -fsSL $docker_mirrors/ubuntu/gpg -o /etc/apt/keyrings/docker.asc sudo chmod a+r /etc/apt/keyrings/docker.asc echo "将存储库添加到 Apt 源" echo \ "deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.asc] $docker_mirrors/ubuntu \ "$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \ tee /etc/apt/sources.list.d/docker.list > /dev/null echo "更新apt包索引" sudo apt-get -y update echo "安装 Docker 引擎、containerd 和 Docker Compose 最新版" sudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
echo "查看 docker-ce 版本 ..." docker version
}
echo "确认系统版本并安装对应 docker" check_sys(){ if [[ -f /etc/redhat-release ]]; then install_docker_ce_centos elif cat /etc/issue | grep -q -E -i "debian|bullseye|bookworm"; then install_docker_ce_debian elif cat /etc/issue | grep -q -E -i "ubuntu|focal|jammy|mantic"; then install_docker_ce_ubuntu elif cat /etc/issue | grep -q -E -i "centos|red hat|redhat"; then install_docker_ce_centos fi }
check_sys
|