#!/bin/bash

# 这个脚本将会自动安装 Docker 和 Docker Compose，并配置国内源

echo "更新 apt 包索引并安装必要的包..."
sudo apt update
sudo apt install -y apt-transport-https ca-certificates curl software-properties-common jq

# 配置阿里云 Docker 镜像源
echo "配置阿里云 Docker 镜像源..."
echo "deb [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/debian $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list

# 添加 Docker 的 GPG 密钥
echo "添加 Docker 的 GPG 密钥..."
curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/debian/gpg | sudo tee /etc/apt/trusted.gpg.d/docker.asc

# 更新 apt 包索引并安装 Docker
echo "更新 apt 包索引并安装 Docker..."
sudo apt update
sudo apt install -y docker-ce docker-ce-cli containerd.io

# 启动并设置 Docker 开机自启
echo "启动 Docker 服务并设置开机自启..."
sudo systemctl start docker
sudo systemctl enable docker

# 验证 Docker 是否安装成功
echo "验证 Docker 安装是否成功..."
docker --version

# 配置 Docker 使用国内镜像加速 (阿里云)
echo "配置 Docker 使用国内镜像加速..."
sudo mkdir -p /etc/docker
echo '{
  "registry-mirrors": ["https://<your-aliyun-id>.mirror.aliyuncs.com"]
}' | sudo tee /etc/docker/daemon.json

# 重启 Docker 服务以应用配置
echo "重启 Docker 服务..."
sudo systemctl restart docker

# 安装 Docker Compose
echo "安装 Docker Compose..."

#sudo curl -L "https://github.com/docker/compose/releases/download/$(curl -s https://api.github.com/repos/docker/compose/releases/latest | jq -r .tag_name)/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

sudo curl -L https://dl.9j6.cn/docker/docker-compose -o /usr/local/bin/docker-compose

# 授予 Docker Compose 执行权限
echo "授予 Docker Compose 执行权限..."
sudo chmod +x /usr/local/bin/docker-compose

# 验证 Docker Compose 是否安装成功
echo "验证 Docker Compose 安装是否成功..."
docker-compose --version

echo "安装完成！"

