ubuntu安装docker和docker-compose
安装docker,使用阿里云镜像源,以下为install_docker.sh文件
sh
# 安装必要的包
echo "安装必要的包..."
sudo apt-get update && sudo apt-get install -y ca-certificates curl gnupg lsb-release
# 添加 Docker 的 GPG 密钥
echo "添加 Docker GPG 密钥..."
curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
# 获取 CPU 架构
ARCHITECTURE=$(uname -m)
echo "检测到 CPU 架构: $ARCHITECTURE"
# 根据架构选择添加 Docker 的 APT 仓库
if [ "$ARCHITECTURE" = "x86_64" ]; then
echo "添加 Docker APT 仓库 (amd64)..."
sudo add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
elif [ "$ARCHITECTURE" = "aarch64" ]; then
echo "添加 Docker APT 仓库 (arm64)..."
sudo add-apt-repository "deb [arch=arm64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
else
echo "不支持的 CPU 架构: $ARCHITECTURE"
exit 1
fi
# 更新系统包列表
echo "更新系统包列表..."
sudo apt-get update
# 安装 Docker
echo "安装 Docker..."
sudo apt-get install -y docker-ce docker-ce-cli containerd.io
# 验证 Docker 安装
echo "验证 Docker 安装..."
sudo docker --version
if [ $? -eq 0 ]; then
echo "Docker安装完成。"
else
echo "Docker安装失败,请检查日志以获取更多信息。"
exit 1
fi
安装docker-compose,以下为install_docker_compose.sh文件
sh
# 输出提示信息
echo "创建目录 /usr/local/bin (如果尚不存在)..."
sudo mkdir -p /usr/local/bin
# 获取 Docker Compose 最新版本
echo "获取 Docker Compose 最新版本号..."
COMPOSE_VERSION=$(curl -s https://api.github.com/repos/docker/compose/releases/latest | grep 'tag_name' | cut -d '"' -f 4)
if [ -z "$COMPOSE_VERSION" ]; then
echo "无法获取 Docker Compose 最新版本号。"
exit 1
fi
echo "最新版本号: $COMPOSE_VERSION"
# 下载 Docker Compose
echo "下载 Docker Compose $COMPOSE_VERSION..."
sudo curl -L "https://github.com/docker/compose/releases/download/${COMPOSE_VERSION}/docker-compose-$(uname -s | tr '[:upper:]' '[:lower:]')-$(uname -m)" -o /usr/local/bin/docker-compose
if [ $? -ne 0 ]; then
echo "下载失败,请检查网络连接或 URL。"
exit 1
fi
# 设置执行权限
echo "设置执行权限..."
sudo chmod +x /usr/local/bin/docker-compose
# 验证安装
echo "验证 Docker Compose 安装..."
sudo docker-compose --version
if [ $? -ne 0 ]; then
echo "Docker Compose 安装失败,请检查日志以获取更多信息。"
exit 1
fi
echo "Docker Compose 安装完成。"
设置docker镜像站,添加三个代理站
shsudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.conf <<EOF
{
"registry-mirrors": [
"https://docker.anyhub.us.kg",
"https://dockerhub.jobcher.com",
"https://dockerhub.icu",
]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
本文作者:phae
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!