Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

docker安装 - ubuntu

准备工作

  • 确认系统类型

    cat /proc/version
    
  • 进一步查看发行版

    lsb_release -a
    
    • 确认是ubuntu,且版本为20.04,代号为focal
  • 检查并卸载旧版本

    sudo apt-get remove docker docker-engine docker.io containerd runc
    

【一键脚本】推荐

curl -sSL https://get.daocloud.io/docker | sh

【其他方法1】用apt-get安装

  • 确认前置依赖的安装

    sudo apt-get install \
                ca-certificates \
                curl \
                gnupg \
                lsb-release
    
  • 配置证书,用于验证软件来源是官方

    sudo mkdir -p /etc/apt/keyrings
    
    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
    
  • 配置docker源,由命令行直接生成,并加入对应文件夹内

    echo \
    "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
    $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
    
    • 自动检测 架构系统代号 填入
  • 更新源

    sudo apt-get update
    
  • 安装默认的最新版本

    sudo apt-get install docker-ce docker-ce-cli containerd.io docker-compose-plugin
    
  • 查询能安装的所有版本

    apt-cache madison docker-ce
    
    • 会按照数字大小排序
    • 如果要安装指定版本
      sudo apt-get install docker-ce=<VERSION_STRING> docker-ce-cli=<VERSION_STRING> containerd.io docker-compose-plugin
      
  • 对于ubuntu等DEB系列的发行版,默认是自动启动,可用如下命令验证

    sudo systemctl status docker
    
  • 测试默认容器能否运行,若正常打印并退出,则安装成功

    sudo docker run hello-world
    
  • 可以用apt自动升级

【其他方法2】用dpkg安装

  • 访问如下网站

    • linux/ubuntu/dists/
    • 选择
      • 系统代号,比如 focal
      • 发行版本,比如 stable
      • 架构,比如 amd64
    • 对应有deb文件,及其下载url
  • 把deb文件下载到本地,最好是安装目录下

  • 执行安装

    sudo dpkg -i /path/to/package.deb
    
  • 默认自动启动,测试默认容器能否运行

    sudo docker run hello-world
    
  • 由于不归apt管理,所以无法自动升级,只能自己手动找到新的deb包,安装后,指向新的路径