install_docker.sh
bash
#!/bin/bash
# 安装 Docker
echo "下载关于Docker的依赖环境"
echo "安装yum-utils包"
sudo yum -y install yum-utils device-mapper-persistent-data lvm2
echo "设置存储库"
sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
sudo yum makecache fast
echo "安装Docker引擎"
sudo yum install docker-ce-23.0.3 docker-ce-cli-23.0.3 containerd.io
# 安装 Docker Compose
sudo mkdir -p /usr/local/bin
sudo chmod 777 /usr/local/bin
sudo rm /usr/local/bin/docker-compose
sudo curl -k -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
# 将用户添加到 docker 用户组中,以免每次执行 Docker 命令都需要 sudo
sudo usermod -aG docker $USER
newgrp docker
# 配置 Docker 镜像加速器
echo "配置 Docker 镜像加速器..."
sudo mkdir -p /etc/docker
echo '{
"registry-mirrors": ["https://mr63yffu.mirror.aliyuncs.com"]
}' | sudo tee /etc/docker/daemon.json
sudo systemctl start docker
# 显示安装结果
echo "Docker 版本:"
docker --version
echo "Docker Compose 版本:"
docker-compose --version
echo "安装完成!"
看到提示确认,直接回车y
uninstall_docker.sh
bash
#!/bin/bash
chmod +x uninstall_docker.sh
# 停止并删除所有正在运行的 Docker 容器
docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q)
# 卸载 Docker 引擎、CLI 和容器运行时
sudo yum remove docker-ce docker-ce-cli containerd.io
# 删除 Docker 创建的所有镜像、容器和卷
sudo rm -rf /var/lib/docker
# 删除 Docker 用户组
sudo groupdel docker
# 删除所有 Docker 相关文件
sudo rm -rf /etc/docker
sudo pip uninstall docker-compose
sudo rm /usr/local/bin/docker-compose
sudo rm /usr/bin/docker-compose
echo "Docker 已成功卸载并清除相关文件。"