# 读取编码 UTF-82.64KB
#!/bin/bash
set -e

handle_error(){
    echo "shell bash faild: $1"
    exit 1
}


# 更新apt  
echo "update apt "
sudo apt-get update || handle_error "update apt failed"

# 安装依赖包
echo "install dependence docker package"
sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg \
    lsb-release || handle_error "install depend package failed"

# 添加docker的apt源
echo "add docker apt source"
curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add - || handle_error "添加Docker密钥失败"

# 添加Docker的阿里云镜像源
echo "添加Docker的阿里云镜像源..."
sudo add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable" || handle_error "添加Docker的阿里云镜像源失败"

# 安装Docker
echo "安装Docker..."
sudo apt-get install -y docker-ce docker-ce-cli containerd.io || handle_error "安装Docker失败"

# 检测Docker是否安装成功
echo "检测Docker是否安装成功..."    
docker --version || handle_error "Docker 未安装成功"

# 启动Docker并设置开机启动
echo "启动Docker并设置开机启动..."
sudo systemctl start docker || handle_error "启动Docker失败"
sudo systemctl enable docker || handle_error "设置Docker开机启动失败"

# 配置docker镜像加速
echo "配置docker镜像加速..."
sudo tee /etc/docker/daemon.json <<-'EOF'
{
    "registry-mirrors": [
        "https://docker.m.daocloud.io",
        "https://docker.imgdb.de",
        "https://docker-0.unsee.tech",
        "https://docker.hlmirror.com",
        "https://docker.1ms.run",
        "https://func.ink",
        "https://lispy.org",
        "https://docker.xiaogenban1993.com"
    ]
}
EOF
echo "Docker 安装完成"


