hunkier

学习笔记,开源代码,技术分享

CentOS 安装 Docker

CentOS 安装 Docker 后,可以方便的使用 Docker 安装其他系统所需的运行环境。

刚安装的CentOS 8服务器,在使用一些常见命令时,发现找不到这些命令,然后使用yum在线安装这些命令时报错:【错误:为仓库 ‘appstream’ 下载元数据失败 : Cannot prepare internal mirrorlist: No URLs in mirrorlist
在网上查阅资料得知,CentOS Linux 8 版本的操作系统结束了生命周期(EOL),Linux 社区不再维护该版本的操作系统,CentOS Linux 8 的镜像及yum源一起迁移到了 https://vault.centos.org

原因已经明确了,CentOS 8 的yum源失效了,那就更新下yum源呗,下边提供几个解决方案供参考。

将yum源改为国内的,方法都类似,以mirrors.aliyun.com 为例:
1)编辑 /etc/yum.repos.d/ 中的相应文件,在 mirrorlist= 开头行前面加 # 注释掉;并将 #baseurl= 开头行取消#注释
2)对于 CentOS 8 之前版本,把域名及路径 mirror.centos.org/centos/$releasever替换为 mirrors.aliyun.com/centos-vault/$minorver
3)对于 CentOS 8 版本,把域名及路径 mirror.centos.org/$contentdir/$releasever替换为 mirrors.aliyun.com/centos-vault/$minorver
$minorver 为 centos 完整版本号,如 CentOS 7 的最后一个版本为 7.9.2009,CentOS 8 的最后一个版本为 8.5.2111)
具体命令如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# Centos 8 之前版本
# 以 CentOS 7.9.2009 为例
minorver=7.9.2009
sudo sed -e "s|^mirrorlist=|#mirrorlist=|g" \
-e "s|^#baseurl=http://mirror.centos.org/centos/\$releasever|baseurl=https://mirrors.aliyun.com/centos-vault/$minorver|g" \
-i.bak \
/etc/yum.repos.d/CentOS-*.repo

# CentOS 8 版本
# 以 CentOS 8.5.2111 为例
minorver=8.5.2111
sudo sed -e "s|^mirrorlist=|#mirrorlist=|g" \
-e "s|^#baseurl=http://mirror.centos.org/\$contentdir/\$releasever|baseurl=https://mirrors.aliyun.com/centos-vault/$minorver|g" \
-i.bak \
/etc/yum.repos.d/CentOS-*.repo

配置文件中,如果需要启用一些 repo,需要将其中的 enabled=0 改为 enabled=1,enabled 省略默认等效为 enabled=1
在切换yum源后,如果之前的yum源在本地有缓存,需要先清理下缓存 yum clean all,然后如果有需要再重新更新缓存 yum makecache
不推荐不同的yum源混用,可能会在软件升级等操作中出现一些未知错误!

安装 Docker 所需要的依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# yum install -y yum-utils device-mapper-persistent-data lvm2
Repository extras is listed more than once in the configuration
Last metadata expiration check: 0:01:28 ago on Fri 12 Jan 2024 10:52:40 AM CST.
Package device-mapper-persistent-data-0.9.0-4.el8.x86_64 is already installed.
Package lvm2-8:2.03.12-10.el8.x86_64 is already installed.
Dependencies resolved.
================================================================================================================================
Package Architecture Version Repository Size
================================================================================================================================
Installing:
yum-utils noarch 4.0.21-3.el8 baseos 73 k

Transaction Summary
================================================================================================================================
Install 1 Package

Total download size: 73 k
Installed size: 22 k
Downloading Packages:
yum-utils-4.0.21-3.el8.noarch.rpm 528 kB/s | 73 kB 00:00
--------------------------------------------------------------------------------------------------------------------------------
Total 522 kB/s | 73 kB 00:00
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : yum-utils-4.0.21-3.el8.noarch 1/1
Running scriptlet: yum-utils-4.0.21-3.el8.noarch 1/1
Verifying : yum-utils-4.0.21-3.el8.noarch 1/1

Installed:
yum-utils-4.0.21-3.el8.noarch

Complete!

添加阿里云仓库

1
2
3
# yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
Repository extras is listed more than once in the configuration
Adding repo from: http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

安装 Docker

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# yum -y install docker-ce
Repository extras is listed more than once in the configuration
Docker CE Stable - x86_64 203 kB/s | 52 kB 00:00
Dependencies resolved.
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
docker-ce x86_64 3:24.0.7-1.el8 docker-ce-stable 24 M
Installing dependencies:
container-selinux noarch 2:2.167.0-1.module_el8.5.0+911+f19012f9
appstream 54 k
containerd.io x86_64 1.6.26-3.1.el8 docker-ce-stable 35 M
docker-ce-cli x86_64 1:24.0.7-1.el8 docker-ce-stable 7.2 M
fuse-common x86_64 3.2.1-12.el8 baseos 21 k
fuse-overlayfs x86_64 1.7.1-1.module_el8.5.0+890+6b136101
appstream 73 k
fuse3 x86_64 3.2.1-12.el8 baseos 50 k
fuse3-libs x86_64 3.2.1-12.el8 baseos 94 k
libcgroup x86_64 0.41-19.el8 baseos 70 k
libslirp x86_64 4.4.0-1.module_el8.5.0+890+6b136101
appstream 70 k
slirp4netns x86_64 1.1.8-1.module_el8.5.0+890+6b136101
appstream 51 k
Installing weak dependencies:
docker-buildx-plugin x86_64 0.11.2-1.el8 docker-ce-stable 13 M
docker-ce-rootless-extras x86_64 24.0.7-1.el8 docker-ce-stable 4.9 M
docker-compose-plugin x86_64 2.21.0-1.el8 docker-ce-stable 13 M
Enabling module streams:
container-tools rhel8

Transaction Summary
================================================================================
Install 14 Packages

Total download size: 97 M
Installed size: 370 M
Downloading Packages:
(1/14): libslirp-4.4.0-1.module_el8.5.0+890+6b1 304 kB/s | 70 kB 00:00
(2/14): container-selinux-2.167.0-1.module_el8. 229 kB/s | 54 kB 00:00
(3/14): fuse-overlayfs-1.7.1-1.module_el8.5.0+8 283 kB/s | 73 kB 00:00
(4/14): slirp4netns-1.1.8-1.module_el8.5.0+890+ 434 kB/s | 51 kB 00:00
(5/14): fuse3-3.2.1-12.el8.x86_64.rpm 405 kB/s | 50 kB 00:00
(6/14): fuse-common-3.2.1-12.el8.x86_64.rpm 122 kB/s | 21 kB 00:00
(7/14): libcgroup-0.41-19.el8.x86_64.rpm 337 kB/s | 70 kB 00:00
(8/14): fuse3-libs-3.2.1-12.el8.x86_64.rpm 337 kB/s | 94 kB 00:00
(9/14): docker-buildx-plugin-0.11.2-1.el8.x86_6 867 kB/s | 13 MB 00:15
(10/14): docker-ce-cli-24.0.7-1.el8.x86_64.rpm 866 kB/s | 7.2 MB 00:08
(11/14): docker-ce-24.0.7-1.el8.x86_64.rpm 856 kB/s | 24 MB 00:28
(12/14): docker-ce-rootless-extras-24.0.7-1.el8.x86_64.rpm 828 kB/s | 4.9 MB 00:06
(13/14): containerd.io-1.6.26-3.1.el8.x86_64.rpm 865 kB/s | 35 MB 00:41
(14/14): docker-compose-plugin-2.21.0-1.el8.x86_64.rpm 876 kB/s | 13 MB 00:14
---------------------------------------------------------------------------------------------------------------------
Total 2.2 MB/s | 97 MB 00:44
Docker CE Stable - x86_64 13 kB/s | 1.6 kB 00:00
Importing GPG key 0x621E9F35:
Userid : "Docker Release (CE rpm) <docker@docker.com>"
Fingerprint: 060A 61C5 1B55 8A7F 742B 77AA C52F EB6B 621E 9F35
From : https://mirrors.aliyun.com/docker-ce/linux/centos/gpg
Key imported successfully
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : docker-compose-plugin-2.21.0-1.el8.x86_64 1/14
Running scriptlet: docker-compose-plugin-2.21.0-1.el8.x86_64 1/14
Running scriptlet: container-selinux-2:2.167.0-1.module_el8.5.0+911+f19012f9.noarch 2/14
Installing : container-selinux-2:2.167.0-1.module_el8.5.0+911+f19012f9.noarch 2/14
Running scriptlet: container-selinux-2:2.167.0-1.module_el8.5.0+911+f19012f9.noarch 2/14
Installing : containerd.io-1.6.26-3.1.el8.x86_64 3/14
Running scriptlet: containerd.io-1.6.26-3.1.el8.x86_64 3/14
Installing : docker-buildx-plugin-0.11.2-1.el8.x86_64 4/14
Running scriptlet: docker-buildx-plugin-0.11.2-1.el8.x86_64 4/14
Installing : docker-ce-cli-1:24.0.7-1.el8.x86_64 5/14
Running scriptlet: docker-ce-cli-1:24.0.7-1.el8.x86_64 5/14
Running scriptlet: libcgroup-0.41-19.el8.x86_64 6/14
Installing : libcgroup-0.41-19.el8.x86_64 6/14
Running scriptlet: libcgroup-0.41-19.el8.x86_64 6/14
Installing : fuse3-libs-3.2.1-12.el8.x86_64 7/14
Running scriptlet: fuse3-libs-3.2.1-12.el8.x86_64 7/14
Installing : fuse-common-3.2.1-12.el8.x86_64 8/14
Installing : fuse3-3.2.1-12.el8.x86_64 9/14
Installing : fuse-overlayfs-1.7.1-1.module_el8.5.0+890+6b136101.x86_64 10/14
Running scriptlet: fuse-overlayfs-1.7.1-1.module_el8.5.0+890+6b136101.x86_64 10/14
Installing : libslirp-4.4.0-1.module_el8.5.0+890+6b136101.x86_64 11/14
Installing : slirp4netns-1.1.8-1.module_el8.5.0+890+6b136101.x86_64 12/14
Installing : docker-ce-rootless-extras-24.0.7-1.el8.x86_64 13/14
Running scriptlet: docker-ce-rootless-extras-24.0.7-1.el8.x86_64 13/14
Installing : docker-ce-3:24.0.7-1.el8.x86_64 14/14
Running scriptlet: docker-ce-3:24.0.7-1.el8.x86_64 14/14
Running scriptlet: container-selinux-2:2.167.0-1.module_el8.5.0+911+f19012f9.noarch 14/14
Running scriptlet: docker-ce-3:24.0.7-1.el8.x86_64 14/14
Verifying : container-selinux-2:2.167.0-1.module_el8.5.0+911+f19012f9.noarch 1/14
Verifying : fuse-overlayfs-1.7.1-1.module_el8.5.0+890+6b136101.x86_64 2/14
Verifying : libslirp-4.4.0-1.module_el8.5.0+890+6b136101.x86_64 3/14
Verifying : slirp4netns-1.1.8-1.module_el8.5.0+890+6b136101.x86_64 4/14
Verifying : fuse-common-3.2.1-12.el8.x86_64 5/14
Verifying : fuse3-3.2.1-12.el8.x86_64 6/14
Verifying : fuse3-libs-3.2.1-12.el8.x86_64 7/14
Verifying : libcgroup-0.41-19.el8.x86_64 8/14
Verifying : containerd.io-1.6.26-3.1.el8.x86_64 9/14
Verifying : docker-buildx-plugin-0.11.2-1.el8.x86_64 10/14
Verifying : docker-ce-3:24.0.7-1.el8.x86_64 11/14
Verifying : docker-ce-cli-1:24.0.7-1.el8.x86_64 12/14
Verifying : docker-ce-rootless-extras-24.0.7-1.el8.x86_64 13/14
Verifying : docker-compose-plugin-2.21.0-1.el8.x86_64 14/14

Installed:
container-selinux-2:2.167.0-1.module_el8.5.0+911+f19012f9.noarch
containerd.io-1.6.26-3.1.el8.x86_64
docker-buildx-plugin-0.11.2-1.el8.x86_64
docker-ce-3:24.0.7-1.el8.x86_64
docker-ce-cli-1:24.0.7-1.el8.x86_64
docker-ce-rootless-extras-24.0.7-1.el8.x86_64
docker-compose-plugin-2.21.0-1.el8.x86_64
fuse-common-3.2.1-12.el8.x86_64
fuse-overlayfs-1.7.1-1.module_el8.5.0+890+6b136101.x86_64
fuse3-3.2.1-12.el8.x86_64
fuse3-libs-3.2.1-12.el8.x86_64
libcgroup-0.41-19.el8.x86_64
libslirp-4.4.0-1.module_el8.5.0+890+6b136101.x86_64
slirp4netns-1.1.8-1.module_el8.5.0+890+6b136101.x86_64

Complete!

启动 Docker

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# systemctl start docker
# docker info
Client: Docker Engine - Community
Version: 24.0.7
Context: default
Debug Mode: false
Plugins:
buildx: Docker Buildx (Docker Inc.)
Version: v0.11.2
Path: /usr/libexec/docker/cli-plugins/docker-buildx
compose: Docker Compose (Docker Inc.)
Version: v2.21.0
Path: /usr/libexec/docker/cli-plugins/docker-compose

Server:
Containers: 0
Running: 0
Paused: 0
Stopped: 0
Images: 0
Server Version: 24.0.7
Storage Driver: overlay2
Backing Filesystem: xfs
Supports d_type: true
Using metacopy: false
Native Overlay Diff: true
userxattr: false
Logging Driver: json-file
Cgroup Driver: cgroupfs
Cgroup Version: 1
Plugins:
Volume: local
Network: bridge host ipvlan macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: io.containerd.runc.v2 runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 3dd1e886e55dd695541fdcd67420c2888645a495
runc version: v1.1.10-0-g18a0cb0
init version: de40ad0
Security Options:
seccomp
Profile: builtin
Kernel Version: 4.18.0-305.3.1.el8.x86_64
Operating System: CentOS Linux 8
OSType: linux
Architecture: x86_64
CPUs: 2
Total Memory: 1.678GiB
Name: iZ7xvfhhgkw6gw7larw760Z
ID: 26a237c9-fb55-4ccb-87ed-6a5881d52537
Docker Root Dir: /var/lib/docker
Debug Mode: false
Experimental: false
Insecure Registries:
127.0.0.0/8
Live Restore Enabled: false

————————————————
参考链接:https://blog.csdn.net/B11050729/article/details/131382641

谢谢你请我喝牛奶

欢迎关注我的其它发布渠道