Debian 安裝 Docker
卸載已有 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 安裝
**具體操作可以參照:替換Debian清華源 清華源-Docker CE 軟件倉庫
-
更換 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 update -y # 更新緩存 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

浙公網安備 33010602011771號