注意:CentOS 7 的安全支持将于2024年6月30日结束,本文内容仅仅是为学习而继续使用CentOS 7,如果你是为了生产环境,或是为了未来的新系统部署Docker环境,则本文部分内容可能不再适用。具体请见 https://blog.centos.org/2023/04/end-dates-are-coming-for-centos-stream-8-and-centos-linux-7/

0.干净的CentOS 7

在安装Docker前需要确认,环境中没有安装Docker。因为已经安装了,就不用安装了。 就像在官方文档中做的那样,先用yum卸载Docker相关的包。

1
2
3
4
5
6
7
8
sudo yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-engine

如果你从前没安装过Docker通常会提示

1
2
3
4
5
6
7
8
参数 docker 没有匹配
参数 docker-client 没有匹配
参数 docker-client-latest 没有匹配
参数 docker-common 没有匹配
参数 docker-latest 没有匹配
参数 docker-latest-logrotate 没有匹配
参数 docker-logrotate 没有匹配
参数 docker-engine 没有匹配

再用systemctl确认

1
  docker.socket                                                          loaded active listening Docker Socket for the API

这一行不存在了,就可以开始安装新版本的Docker了。

注意:卸载Docker,你原有的Docker配置不会自动删除,如有必要,可前往/var/lib/docker/删除配置

1.安装Docker

官方文档中Docker有多种安装方式,本次我们以从rpm安装为例。

首先执行下面的命令

1
2
sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

提示

1
2
3
adding repo from: https://download.docker.com/linux/centos/docker-ce.repo
grabbing file https://download.docker.com/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo
repo saved to /etc/yum.repos.d/docker-ce.repo

即表明你正确的添加了Docker的源。

执行

1
sudo yum install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

在提示Is this ok [y/d/N]: 是选择y,接下来你可能会看到Docker的GPG密钥添加提示

1
2
3
4
5
6
从 https://download.docker.com/linux/centos/gpg 检索密钥
导入 GPG key 0x621E9F35:
 用户ID     : "Docker Release (CE rpm) <docker@docker.com>"
 指纹       : 060a 61c5 1b55 8a7f 742b 77aa c52f eb6b 621e 9f35
 来自       : https://download.docker.com/linux/centos/gpg
是否继续?[y/N]:y

通常情况下我们的Docker GPG指纹应该是一样的,如果不一样则有可能是中间人劫持,一样的话填y就行。

安装完成后Docker不会自动运行,请使用sudo systemctl start docker 命令启动Docker。

运行sudo docker run hello-world 查看终端是否输出

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
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在CentOS 7上安装成功。

2.Docker镜像配置

有时你可能会发现docker pull命令不好使,总是卡在一个百分比上。可以尝试在/etc/docker/下创建一个daemon.json文件。内容为

1
2
3
4
5
6
{
  "registry-mirrors": [
    "https://hub-mirror.c.163.com",
    "https://mirror.baidubce.com"
  ]
}

然后使用sudo systemctl daemon-reload重新加载daemon,sudo systemctl restart docker重启Docker,使用sudo docker info查看Docker状态。

看到终端输出中有

1
2
3
Registry Mirrors:
  https://hub-mirror.c.163.com/
  https://mirror.baidubce.com/

即表明Docker Mirror配置成功。

3.配置Docker开机自启

输入sudo systemctl enable docker.service即可,如希望Docker镜像也能够自启可以参照 https://blog.csdn.net/qq_37312838/article/details/115710209 的方法解决。

感谢

https://docker-practice.github.io/zh-cn/install/mirror.html 提供的docker mirror地址