#!/bin/bash
set -e
# 卸载旧版本
TO_REMOVE=$(yum list installed | grep -E 'docker|docker-client|docker-client-latest|docker-common|docker-latest|docker-latest-logrotate|docker-logrotate|docker-engine' | awk '{ print $1 }' || true)
if [ -n "$TO_REMOVE" ]; then
sudo yum remove -y $TO_REMOVE || exit 1
fi
# 安装 Docker CE
sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
sudo yum install -y docker-ce-20.10.6 docker-ce-cli-20.10.6 containerd.io
# 启动 Docker
sudo systemctl start docker
sudo systemctl enable docker
# 安装 Docker Compose
for i in {1..5}; do
curl -L "https://github.com/docker/compose/releases/download/1.25.5/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose && break || {
echo "下载失败,等待 15 秒后重试..."
sleep 15;
}
done
sudo chmod +x /usr/local/bin/docker-compose
# 添加 Docker 镜像源和其他设置
sudo mkdir -p /etc/docker
echo '{
"registry-mirrors": ["https://uudccx3q.mirror.aliyuncs.com","https://docker.mirrors.ustc.edu.cn/"],
"dns": ["8.8.8.8", "8.8.4.4"],
"default-address-pools": [{"base": "10.10.0.0/16", "size": 24}]
}' | sudo tee /etc/docker/daemon.json
# 重启 Docker 以应用更改
sudo systemctl restart docker
# 验证 Docker 安装
DOCKER_VERSION=$(docker --version)
if [[ $DOCKER_VERSION == *"20.10.6"* ]]; then
echo "Docker 安装成功,版本为 $DOCKER_VERSION"
else
echo "Docker 安装失败,退出脚本"
exit 1
fi
# 验证 Docker Compose 安装
DOCKER_COMPOSE_VERSION=$(docker-compose --version)
if [[ $DOCKER_COMPOSE_VERSION == *"1.25.5"* ]]; then
echo "Docker Compose 安装成功,版本为 $DOCKER_COMPOSE_VERSION"
else
echo "Docker Compose 安装失败,退出脚本"
exit 1
fi
echo "Docker 和 Docker Compose 安装成功。"
一键式安装docker(20.10.6)与docker-compose(1.25.5)
于 2023-11-06 22:18:36 首次发布