卸载已有 Docker
如果你之前安装过 Docker Engine 之前,你需要卸载旧版本,避免冲突:
for pkg in docker.io docker-doc docker-compose podman-docker containerd runc; do sudo apt-get remove $pkg; done
安装方法
使用官方安装脚本自动安装 (推荐使用)
-
下载官方脚本
curl -fsSL https://get.docker.com -o get-docker.sh
-
执行脚本
sudo sh get-docker.sh
官方 apt 源安装
如果你的网络无法连接到官方网站的话,那可以使用 apt 源手动安装
-
更新 apt 源
sudo apt update -y # 更新 apt 缓存 # sudo apt upgrade -y # 安装新版本依赖
-
安装下载 https 依赖包
sudo apt install -y apt-transport-https ca-certificates curl software-properties-common
-
添加 官方 GPC 密钥
sudo curl -fsSL https://download.docker.com/linux/debian/gpg -o /etc/apt/keyrings/docker.asc # 下载 GPC 密钥到本地 sudo chmod a+r /etc/apt/keyrings/docker.asc # 将 GPC 密钥文件的读取权限添加给所有用户
-
添加 Docker 官方仓库
添加 apt 源到本地仓库
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/debian \ $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
-
更新 apt 缓存以保障使用步骤4添加源
sudo apt update -y
-
验证仓库添加成功
执行以下命令查看返回结果
apt-cache policy docker-ce
如果返回的结果包含
https://download.docker.com/linux/debian
,则是添加成功。 -
安装 Docker
sudo apt install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
清华源 apt 安装
-
更换 apt 源为清华源
sudo mv /etc/apt/sources.list /etc/apt/sources.list.bak # 备份原有 apt源
-
如果你的 Debian 版本是 12,则执行以下命令
sudo tee /etc/apt/sources.list << EOF # 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释 deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm main contrib non-free non-free-firmware # deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm main contrib non-free non-free-firmware deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-updates main contrib non-free non-free-firmware # deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-updates main contrib non-free non-free-firmware deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-backports main contrib non-free non-free-firmware # deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-backports main contrib non-free non-free-firmware # 以下安全更新软件源包含了官方源与镜像站配置,如有需要可自行修改注释切换 deb https://security.debian.org/debian-security bookworm-security main contrib non-free non-free-firmware # deb-src https://security.debian.org/debian-security bookworm-security main contrib non-free non-free-firmware EOF
-
如果你的 Debian 版本是 11,则执行以下命令
sudo tee /etc/apt/sources.list << EOF # 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释 deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye main contrib non-free # deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye main contrib non-free deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-updates main contrib non-free # deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-updates main contrib non-free deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-backports main contrib non-free # deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-backports main contrib non-free # 以下安全更新软件源包含了官方源与镜像站配置,如有需要可自行修改注释切换 deb https://security.debian.org/debian-security bullseye-security main contrib non-free # deb-src https://security.debian.org/debian-security bullseye-security main contrib non-free EOF
-
-
安装验证密钥依赖
sudo apt install -y curl gnupg2
-
添加清华源 Docker GPC 密钥
curl -fsSL https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/debian/gpg |sudo apt-key add -
-
添加清华 Docker 源
echo "deb https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/debian bookworm stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
-
更新 apt 缓存以保障使用步骤4添加源
sudo apt update -y
-
安装 Docker
sudo apt install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
启动并验证 Docker
-
启动 Docker 设置开机启动
sudo systemctl start docker # 启动 Docker sudo systemctl enable docker # 设置开机启动
-
查看 Docker 版本
sudo docker --version
-
拉取 测试镜像验证
sudo docker run hello-world
取消非 root 用户需要 sudo
-
创建 docker 组
sudo groupadd docker
-
将当前用户加入 docker 组
sudo usermod -aG docker $USER
如果给其他用户添加权限则把 $USER 修改为用户名,例如: zhangsan
sudo usermod -aG docker zhangsan
-
刷新docker成员
newgrp docker
-
重启服务
sudo systemctl restart docker # 重启 docker
-
验证
docker ps -a
卸载 Docker
-
卸载 Docker
for pkg in docker.io docker-doc docker-compose podman-docker containerd runc; do sudo apt-get remove $pkg; done
-
删除 Docker 目录
sudo rm -rf /var/lib/docker