Ubuntu

警告:切勿在没有配置 Docker APT 源的情况下直接使用 apt 命令安装 Docker.

准备工作

系统要求

Docker 支持以下版本的 Ubuntu 操作系统:

  • Ubuntu Hirsute 21.04
  • Ubuntu Groovy 20.10
  • Ubuntu Focal 20.04 (LTS)
  • Ubuntu Bionic 18.04 (LTS)

Docker 可以安装在 64 位的 x86 平台或 ARM 平台上。Ubuntu 发行版中,LTS(Long-Term-Support)长期支持版本,会获得 5 年的升级维护支持,这样的版本会更稳定,因此在生产环境中推荐使用 LTS 版本。

卸载旧版本

旧版本的 Docker 称为 docker​ 或者 docker-engine​,使用以下命令卸载旧版本:

$ sudo apt-get remove docker \
               docker-engine \
               docker.io

使用 APT 安装

由于 apt​ 源使用 HTTPS 以确保软件下载过程中不被篡改。因此,我们首先需要添加使用 HTTPS 传输的软件包以及 CA 证书。

$ sudo apt-get update

$ sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg \
    lsb-release

鉴于国内网络问题,强烈建议使用国内源,官方源请在注释中查看。

为了确认所下载软件包的合法性,需要添加软件源的 GPG​ 密钥。

$ curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg


# 官方源
# $ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

然后,我们需要向 sources.list​ 中添加 Docker 软件源

$ echo \
  "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://mirrors.aliyun.com/docker-ce/linux/ubuntu \
  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null


# 官方源
# $ echo \
#   "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
#   $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

以上命令会添加稳定版本的 Docker APT 镜像源,如果需要测试版本的 Docker 请将 stable 改为 test。

安装 Docker

更新 apt 软件包缓存,并安装 docker-ce​:

$ sudo apt-get update

$ sudo apt-get install docker-ce docker-ce-cli containerd.io

使用脚本自动安装

在测试或开发环境中 Docker 官方为了简化安装流程,提供了一套便捷的安装脚本,Ubuntu 系统上可以使用这套脚本安装,另外可以通过 --mirror​ 选项使用国内源进行安装:

若你想安装测试版的 Docker, 请从 test.docker.com 获取脚本

# $ curl -fsSL test.docker.com -o get-docker.sh
$ curl -fsSL get.docker.com -o get-docker.sh
$ sudo sh get-docker.sh --mirror Aliyun
# $ sudo sh get-docker.sh --mirror AzureChinaCloud

执行这个命令后,脚本就会自动的将一切准备工作做好,并且把 Docker 的稳定(stable)版本安装在系统中。

启动 Docker

$ sudo systemctl enable docker
$ sudo systemctl start docker

建立 docker 用户组

默认情况下,docker​ 命令会使用 Unix socket 与 Docker 引擎通讯。而只有 root​ 用户和 docker​ 组的用户才可以访问 Docker 引擎的 Unix socket。出于安全考虑,一般 Linux 系统上不会直接使用 root​ 用户。因此,更好地做法是将需要使用 docker​ 的用户加入 docker​ 用户组。

建立 docker​ 组:

$ sudo groupadd docker

将当前用户加入 docker​ 组:

$ sudo usermod -aG docker $USER

退出当前终端并重新登录,进行如下测试。

测试 Docker 是否安装正确

$ docker run --rm hello-world

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
b8dfde127a29: Pull complete
Digest: sha256:308866a43596e83578c7dfa15e27a73011bdd402185a84c5cd7f32a88b501a24
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

若能正常输出以上信息,则说明安装成功。

镜像加速

如果在使用过程中发现拉取 Docker 镜像十分缓慢,可以配置 Docker 国内镜像加速1

参考文档


  1. 镜像加速器

    国内从 Docker Hub 拉取镜像有时会遇到困难,此时可以配置镜像加速器。国内很多云服务商都提供了国内加速器服务,例如:

    **由于镜像服务可能出现宕机,建议同时配置多个镜像。各个镜像站测试结果请到 docker-practice/docker-registry-cn-mirror-test​ ** 查看。

    国内各大云服务商(腾讯云、阿里云、百度云)均提供了 Docker 镜像加速服务,建议根据运行 Docker 的云平台选择对应的镜像加速服务,具体请参考本页最后一小节。

    本节我们以 网易云 镜像服务 https://hub-mirror.c.163.com​ 为例进行介绍。

    Ubuntu 16.04+、Debian 8+、CentOS 7+

    目前主流 Linux 发行版均已使用 systemd 进行服务管理,这里介绍如何在使用 systemd 的 Linux 发行版中配置镜像加速器。

    请首先执行以下命令,查看是否在 docker.service​ 文件中配置过镜像地址。

    $ systemctl cat docker | grep '\-\-registry\-mirror'
    

    如果该命令有输出,那么请执行 $ systemctl cat docker​ 查看 ExecStart=​ 出现的位置,修改对应的文件内容去掉 --registry-mirror​ 参数及其值,并按接下来的步骤进行配置。

    如果以上命令没有任何输出,那么就可以在 /etc/docker/daemon.json​ 中写入如下内容(如果文件不存在请新建该文件):

    {
      "registry-mirrors": [
        "https://hub-mirror.c.163.com",
        "https://mirror.baidubce.com"
      ]
    }
    

    注意,一定要保证该文件符合 json 规范,否则 Docker 将不能启动。

    之后重新启动服务。

    $ sudo systemctl daemon-reload
    $ sudo systemctl restart docker
    

    Windows 10

    对于使用 Windows 10​ 的用户,在任务栏托盘 Docker 图标内右键菜单选择 Settings​,打开配置窗口后在左侧导航菜单选择 Docker Engine​,在右侧像下边一样编辑 json 文件,之后点击 Apply & Restart​ 保存后 Docker 就会重启并应用配置的镜像地址了。

    {
      "registry-mirrors": [
        "https://hub-mirror.c.163.com",
        "https://mirror.baidubce.com"
      ]
    }
    

    macOS

    对于使用 macOS 的用户,在任务栏点击 Docker Desktop 应用图标 -> Settings...​,在左侧导航菜单选择 Docker Engine​,在右侧像下边一样编辑 json 文件。修改完成之后,点击 Apply & restart​ 按钮,Docker 就会重启并应用配置的镜像地址了。

    {
      "registry-mirrors": [
        "https://hub-mirror.c.163.com",
        "https://mirror.baidubce.com"
      ]
    }
    

    检查加速器是否生效

    执行 $ docker info​,如果从结果中看到了如下内容,说明配置成功。

    Registry Mirrors:
     https://hub-mirror.c.163.com/
    

    k8s.gcr.io​ 镜像

    可以登录 阿里云 容器镜像服务 镜像中心 -> 镜像搜索 查找。

    例如 k8s.gcr.io/coredns:1.6.7​ 镜像可以用 registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:1.6.7​ 代替。

    一般情况下有如下对应关系:

    # $ docker pull k8s.gcr.io/xxx
    
    $ docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/xxx
    

    不再提供服务的镜像

    某些镜像不再提供服务,添加无用的镜像加速器,会拖慢镜像拉取速度,你可以从镜像配置列表中删除它们。

    建议 watch(页面右上角) 镜像测试 这个 GitHub 仓库,我们会在此更新各个镜像地址的状态。

    云服务商

    某些云服务商提供了 仅供内部 访问的镜像服务,当您的 Docker 运行在云平台时可以选择它们。

  • Ubuntu

    Ubuntu(友帮拓、优般图、乌班图)是一个以桌面应用为主的 Linux 操作系统,其名称来自非洲南部祖鲁语或豪萨语的“ubuntu”一词,意思是“人性”、“我的存在是因为大家的存在”,是非洲传统的一种价值观,类似华人社会的“仁爱”思想。Ubuntu 的目标在于为一般用户提供一个最新的、同时又相当稳定的主要由自由软件构建而成的操作系统。

    123 引用 • 168 回帖

相关帖子

回帖

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...