CentOS / Ubuntu 下安装 Docker

2020-04-02 11:20:17
李文睿
35396
最后编辑:丁永亮 于 2022-01-25 16:37:36
分享链接

本文描述了如何在 CentOS 或 Ubuntu 下使用 清华大学开源软件镜像站 提供的镜像安装 Docker。

 

前提条件

Ubuntu

系统要求为以下版本的 64 位版本:Ubuntu 19.10 / Ubuntu 18.04 / Ubuntu 16.04。

CentOS

系统版本为 CentOS 7 或 8;需要启用centos-extras软件仓库,该仓库默认是启用的,如果被禁用可以参考 CentOS 官方文档 启用这个仓库;在 CentOS 下建议使用overlay2作为文件存储驱动。

 

卸载旧版本

Ubuntu

需要执行以下命令卸载系统中的docker、docker.io或docker-engine包:

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

 

CentOS

需要执行以下命令卸载系统中的docker或docker-engine包:

$ sudo yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-engine

 

安装 Docker

Ubuntu

在安装之前,需要添加 Docker 软件仓库:

  1. 运行sudo apt-get update更新包索引;

  2. 安装 apt 的 HTTPS 依赖:

    $ sudo apt-get install apt-transport-https \
                           ca-certificates \
                           curl \
                           gnupg-agent \
                           software-properties-common
  3. 添加 Docker 官方公钥:

    $ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
  4. 添加清华大学开源软件镜像站的 Docker 软件镜像源:

    $ sudo add-apt-repository \
       "deb [arch=amd64] https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu \
       $(lsb_release -cs) \
       stable"
  5. 添加软件源后再次运行sudo apt-get update更新包索引;

安装 Docker:

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

 

CentOS

在安装之前,需要添加 Docker 软件仓库:

  1. 安装所需的软件包:

    $ sudo yum install -y yum-utils \
                          device-mapper-persistent-data \
                          lvm2
  2. 添加稳定分支的 Docker 软件源:

    $ sudo yum-config-manager \
       --add-repo \
       https://download.docker.com/linux/centos/docker-ce.repo
  3. 将软件仓库地址替换为清华大学开源软件镜像站地址:

    $ sudo sed -i 's+download.docker.com+mirrors.tuna.tsinghua.edu.cn/docker-ce+' \
       /etc/yum.repos.d/docker-ce.repo
  4. 运行sudo yum makecache fast更新本地包索引缓存;

安装 Docker:

$ sudo yum install docker-ce docker-ce-cli containerd.io

启动 Docker 服务:

$ sudo systemctl start docker

 

验证 Docker 安装成功

安装完成后尝试运行 Docker 的 hello-world 镜像以验证安装:

$ sudo docker run hello-world

该镜像运行后会输出以下文本:

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 安装成功。

发表评论
伍 加 贰 =
评论通过审核后显示。