CentOS 7 安装 Docker

准备工作

系统要求

Docker CE 支持 64 位版本 CentOS 7,并且要求内核版本不低于 3.10。 CentOS 7 满足最低内核的要求,但由于内核版本比较低,部分功能无法使用,并且部分功能可能不太稳定。

1、首先要确定是否是 CenOS 7 版本以上

$ sudo cat /etc/redhat-release

2、确定内核是否是3.1.0以上

$ sudo uname -a

安装 gcc 相关工具包

$ sudo yum -y install gcc
$ sudo yum -y install gcc c++

# 查看是否安装成功
$ sudo gcc -v

卸载 Docker 旧版本

较旧版本的Docker引擎,如果已安装这些,需要先卸载它们以及相关的依赖项。

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

# 提示没有安装这些软件包,则OK,否则将会删除 /var/lib/docker/ 目录的数据,
# 这个目录包含了镜像、容器、卷和网络等相关数据和配置

使用 yum 安装需要的包

$ sudo yum install -y yum-utils \
  device-mapper-persistent-data \
  lvm2

设置Stable镜像仓库

当前官方提供的国外源,很容器出现网络问题,造成安装失败,鉴于国内网络问题,我们使用国内阿里源。

$ sudo yum-config-manager \
    --add-repo \
    http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo


# 官方源
# $ sudo yum-config-manager \
#     --add-repo \
#     https://download.docker.com/linux/centos/docker-ce.repo 

安装 Docker CE

更新 yum 软件源缓存,并安装 docker-ce

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

启动 Docker CE

$ sudo systemctl start docker

测试 Docker 是否安装正确

$ sudo docker run hello-world

# 此命令下载测试映像并在容器中运行它。当容器运行时,它会打印一条信息 Hello from Docker! 并退出。
# 说明安装成功!

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete 
Digest: sha256:6540fc08ee6e6b7b63468dc3317e3303aae178cb8a45ed3123180328bcc1d20f
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/

镜像加速

ps: 鉴于国内网络问题,后续拉取 Docker 镜像十分缓慢,强烈建议安装 Docker 之后配置 国内镜像加速

  • 通过修改 daemon 配置文件 /etc/docker/daemon.json 来使用加速器
$ sudo mkdir -p /etc/docker
  • 使用阿里云镜像源
$ sudo vi /etc/docker/daemon.json

# 添加一下镜像源
{
  "registry-mirrors": ["https://[编码id].mirror.aliyuncs.com"]
}

# 编码id 通过 https://cr.console.aliyun.com 获取阿里云镜像源地址

重启docker

$ sudo systemctl daemon-reload
$ sudo systemctl restart docker

卸载docker

$ sudo systemctl stop docker
$ sudo yum -y remove docker-ce
$ sudo rm -rf /var/lib/docke 

参考文档

Docker 官方 CentOS 安装文档

上次更新: 2020-5-10 1:34:13 PM