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

安装方法

使用官方安装脚本自动安装 (推荐使用)

  1. 下载官方脚本

     curl -fsSL https://get.docker.com -o get-docker.sh
    
  2. 执行脚本

    sudo sh get-docker.sh
    

官方 apt 源安装

如果你的网络无法连接到官方网站的话,那可以使用 apt 源手动安装

  1. 更新 apt 源

    sudo apt update -y  # 更新 apt 缓存
    

    如果你的服务器长时间未更新,建议使用 sudo apt upgrade -y 更新一下软件版本 注意:生产环境不要执行这个操作,除非你确定软件升级没有影响

  2. 安装下载 https 依赖包

    sudo apt install -y apt-transport-https ca-certificates curl software-properties-common
    
  3. 添加 官方 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 密钥文件的读取权限添加给所有用户
    
  4. 添加 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
    
  5. 更新 apt 缓存以保障使用步骤4添加源

    sudo apt update -y
    
  6. 验证仓库添加成功

    执行以下命令查看返回结果

    apt-cache policy docker-ce
    

    如果返回的结果包含https://download.docker.com/linux/debian,则是添加成功。

  7. 安装 Docker

    sudo apt install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
    

清华源 apt 安装

**具体操作可以参照:替换Debian清华源 清华源-Docker CE 软件仓库

  1. 更换 apt 源为清华源

    sudo mv /etc/apt/sources.list /etc/apt/sources.list.bak  # 备份原有 apt源
    
    1. 如果你的 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
      
    2. 如果你的 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
      
  2. 安装验证密钥依赖

    sudo apt update -y # 更新缓存
    sudo apt install -y curl gnupg2  # 安装依赖
    
  3. 添加清华源 Docker GPC 密钥

    curl -fsSL https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/debian/gpg |sudo apt-key add -
    
  4. 添加清华 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  
    
  5. 更新 apt 缓存以保障使用步骤4添加源

    sudo apt update -y
    
  6. 安装 Docker

    sudo apt install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
    

启动并验证 Docker

  1. 启动 Docker 设置开机启动

    sudo systemctl start docker # 启动 Docker
    sudo systemctl enable docker # 设置开机启动
    
  2. 查看 Docker 版本

    sudo docker --version
    
  3. 拉取 测试镜像验证

    sudo docker run hello-world
    

取消非 root 用户需要 sudo

  1. 创建 docker 组

    sudo groupadd docker
    
  2. 将当前用户加入 docker 组

    sudo usermod -aG docker $USER
    

    如果给其他用户添加权限则把 $USER 修改为用户名,例如: zhangsan

    sudo usermod -aG docker zhangsan
    
  3. 刷新docker成员

    newgrp docker
    
  4. 重启服务

    sudo systemctl restart docker # 重启 docker
    
  5. 验证

    docker ps -a
    

卸载 Docker

  1. 卸载 Docker

    for pkg in docker.io docker-doc docker-compose podman-docker containerd runc; do sudo apt-get remove $pkg; done
    
  2. 删除 Docker 目录

    sudo rm -rf /var/lib/docker
    
posted @ 2025-01-15 16:51  神奇小宝  阅读(3221)  评论(1)    收藏  举报