Script/install-docker.sh
qin 419359928d 上传文件至 /
阿里云docker源
2024-11-11 06:22:37 +00:00

88 lines
2.3 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# 检查是否以root权限运行
if [ "$EUID" -ne 0 ]; then
echo "请以root权限运行此脚本"
exit 1
fi
# 打印开始信息
echo "开始安装Docker..."
echo "-------------------"
# 卸载旧版本Docker如果存在
echo "正在删除旧版本Docker..."
apt-get remove docker docker-ce docker.io containerd runc -y
# 更新apt包索引
echo "更新apt包索引..."
apt-get update
# 安装必要的系统工具
echo "安装必要的系统工具..."
apt-get install -y \
apt-transport-https \
ca-certificates \
curl \
gnupg \
lsb-release
# 添加Docker的官方GPG密钥
echo "添加Docker的官方GPG密钥..."
curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/debian/gpg | gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
# 设置稳定版仓库
echo "设置Docker仓库..."
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://mirrors.aliyun.com/docker-ce/linux/debian \
$(lsb_release -cs) stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null
# 显示源配置信息
echo "当前系统配置的源:"
cat /etc/apt/sources.list
echo "Docker源配置"
cat /etc/apt/sources.list.d/docker.list
# 更新apt包索引
echo "更新apt包索引..."
apt-get update
# 安装Docker Engine
echo "安装Docker Engine..."
apt-get install -y docker-ce docker-ce-cli containerd.io
# 配置Docker镜像加速器
echo "配置镜像加速器..."
mkdir -p /etc/docker
tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://mirror.ccs.tencentyun.com"]
}
EOF
# 启动Docker服务
echo "启动Docker服务..."
systemctl daemon-reload
systemctl start docker
systemctl enable docker
# 验证Docker安装
echo "验证Docker安装..."
docker --version
# 运行hello-world镜像测试
echo "测试Docker安装..."
docker run hello-world
# 添加当前用户到docker组
if [ -n "$SUDO_USER" ]; then
echo "将用户 $SUDO_USER 添加到docker组..."
usermod -aG docker $SUDO_USER
echo "请注销并重新登录以使组权限生效"
fi
echo "-------------------"
echo "Docker安装完成"
echo "可以使用 'docker --version' 查看版本"
echo "使用 'docker ps' 查看运行中的容器"
echo "使用 'docker images' 查看本地镜像"