Docker实践之Linux安装Docker

说到Docker,便要追溯到前些年,我用公司的模块直接从k8s+kubesphere就干起来了,然而,用了这么多年的k8s,却对Docker不甚了解,这便想着,得抽时间从最基础开始实践。

这一节就从Docker实践之Linux安装Docker开始,我的Linux是版本是CentOS 8.2,

1
2
3
4
[root@VM-12-3-centos ~]# cat /proc/version
Linux version 4.18.0-305.3.1.el8.x86_64 (mockbuild@kbuilder.bsys.centos.org) (gcc version 8.4.1 20200928 (Red Hat 8.4.1-1) (GCC)) #1 SMP Tue Jun 1 16:14:33 UTC 2021
[root@VM-12-3-centos ~]# cat /etc/redhat-release
CentOS Linux release 8.2.2004 (Core)

配置yum源地址

为什么要配置yum源地址呢,因为yum源地址在国外,不利于我们访问,所以,我们需要找一个国内的代理镜像地址来加速我们访问,如:

  • (阿里巴巴)http://mirrors.aliyun.com/centos/
  • (网易)http://mirrors.163.com/
  • (清华大学)https://mirrors.tuna.tsinghua.edu.cn/
    在配置之前,我们先安装一个工具yum-utils,在Linux执行yum即可,yum install -y yum-utils
    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
    [root@VM-12-3-centos ~]# yum install -y yum-utils
    Last metadata expiration check: 2:17:09 ago on Fri 17 Dec 2021 10:36:25 AM CST.
    Dependencies resolved.
    =================================================================================================================================================================================================================
    Package Architecture Version Repository Size
    =================================================================================================================================================================================================================
    Installing:
    yum-utils noarch 4.0.21-3.el8 BaseOS 73 k
    Upgrading:
    dnf-plugins-core noarch 4.0.21-3.el8 BaseOS 70 k
    python3-dnf-plugins-core noarch 4.0.21-3.el8 BaseOS 234 k

    Transaction Summary
    =================================================================================================================================================================================================================
    Install 1 Package
    Upgrade 2 Packages

    Total download size: 377 k
    Downloading Packages:
    (1/3): dnf-plugins-core-4.0.21-3.el8.noarch.rpm 523 kB/s | 70 kB 00:00
    (2/3): yum-utils-4.0.21-3.el8.noarch.rpm 319 kB/s | 73 kB 00:00
    (3/3): python3-dnf-plugins-core-4.0.21-3.el8.noarch.rpm 1.0 MB/s | 234 kB 00:00
    -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    Total 1.6 MB/s | 377 kB 00:00
    Running transaction check
    Transaction check succeeded.
    Running transaction test
    Transaction test succeeded.
    Running transaction
    Preparing : 1/1
    Upgrading : python3-dnf-plugins-core-4.0.21-3.el8.noarch 1/5
    Upgrading : dnf-plugins-core-4.0.21-3.el8.noarch 2/5
    Installing : yum-utils-4.0.21-3.el8.noarch 3/5
    Cleanup : dnf-plugins-core-4.0.18-4.el8.noarch 4/5
    Cleanup : python3-dnf-plugins-core-4.0.18-4.el8.noarch 5/5
    Running scriptlet: python3-dnf-plugins-core-4.0.18-4.el8.noarch 5/5
    Verifying : yum-utils-4.0.21-3.el8.noarch 1/5
    Verifying : dnf-plugins-core-4.0.21-3.el8.noarch 2/5
    Verifying : dnf-plugins-core-4.0.18-4.el8.noarch 3/5
    Verifying : python3-dnf-plugins-core-4.0.21-3.el8.noarch 4/5
    Verifying : python3-dnf-plugins-core-4.0.18-4.el8.noarch 5/5

    Upgraded:
    dnf-plugins-core-4.0.21-3.el8.noarch python3-dnf-plugins-core-4.0.21-3.el8.noarch
    Installed:
    yum-utils-4.0.21-3.el8.noarch

    Complete!

这里我们配置阿里的yum源

1
2
3
[root@VM-12-3-centos ~]# yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
Adding repo from: https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

安装Docker

目前Docker分ce(社区版)和ee(企业版),这里当然是安装Docker-ce(社区版)了,执行安装命令yum install docker-ce docker-ce-cli containerd.io

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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
[root@VM-12-3-centos ~]# yum install -y yum-utils
Last metadata expiration check: 2:17:09 ago on Fri 17 Dec 2021 10:36:25 AM CST.
Dependencies resolved.
=================================================================================================================================================================================================================
Package Architecture Version Repository Size
=================================================================================================================================================================================================================
Installing:
yum-utils noarch 4.0.21-3.el8 BaseOS 73 k
Upgrading:
dnf-plugins-core noarch 4.0.21-3.el8 BaseOS 70 k
python3-dnf-plugins-core noarch 4.0.21-3.el8 BaseOS 234 k

Transaction Summary
=================================================================================================================================================================================================================
Install 1 Package
Upgrade 2 Packages

Total download size: 377 k
Downloading Packages:
(1/3): dnf-plugins-core-4.0.21-3.el8.noarch.rpm 523 kB/s | 70 kB 00:00
(2/3): yum-utils-4.0.21-3.el8.noarch.rpm 319 kB/s | 73 kB 00:00
(3/3): python3-dnf-plugins-core-4.0.21-3.el8.noarch.rpm 1.0 MB/s | 234 kB 00:00
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total 1.6 MB/s | 377 kB 00:00
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Upgrading : python3-dnf-plugins-core-4.0.21-3.el8.noarch 1/5
Upgrading : dnf-plugins-core-4.0.21-3.el8.noarch 2/5
Installing : yum-utils-4.0.21-3.el8.noarch 3/5
Cleanup : dnf-plugins-core-4.0.18-4.el8.noarch 4/5
Cleanup : python3-dnf-plugins-core-4.0.18-4.el8.noarch 5/5
Running scriptlet: python3-dnf-plugins-core-4.0.18-4.el8.noarch 5/5
Verifying : yum-utils-4.0.21-3.el8.noarch 1/5
Verifying : dnf-plugins-core-4.0.21-3.el8.noarch 2/5
Verifying : dnf-plugins-core-4.0.18-4.el8.noarch 3/5
Verifying : python3-dnf-plugins-core-4.0.21-3.el8.noarch 4/5
Verifying : python3-dnf-plugins-core-4.0.18-4.el8.noarch 5/5

Upgraded:
dnf-plugins-core-4.0.21-3.el8.noarch python3-dnf-plugins-core-4.0.21-3.el8.noarch
Installed:
yum-utils-4.0.21-3.el8.noarch

Complete!
[root@VM-12-3-centos ~]# yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
Adding repo from: https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
[root@VM-12-3-centos ~]# yum install docker-ce docker-ce-cli containerd.io
Docker CE Stable - x86_64 278 kB/s | 19 kB 00:00
Last metadata expiration check: 0:00:01 ago on Fri 17 Dec 2021 01:32:08 PM CST.
Dependencies resolved.
=================================================================================================================================================================================================================
Package Architecture Version Repository Size
=================================================================================================================================================================================================================
Installing:
containerd.io x86_64 1.4.12-3.1.el8 docker-ce-stable 28 M
docker-ce x86_64 3:20.10.12-3.el8 docker-ce-stable 22 M
docker-ce-cli x86_64 1:20.10.12-3.el8 docker-ce-stable 30 M
Installing dependencies:
container-selinux noarch 2:2.167.0-1.module_el8.5.0+911+f19012f9 AppStream 54 k
docker-ce-rootless-extras x86_64 20.10.12-3.el8 docker-ce-stable 4.6 M
docker-scan-plugin x86_64 0.12.0-3.el8 docker-ce-stable 3.7 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
Enabling module streams:
container-tools rhel8

Transaction Summary
=================================================================================================================================================================================================================
Install 13 Packages

Total download size: 90 M
Installed size: 378 M
Is this ok [y/N]: y
Downloading Packages:
(1/13): libslirp-4.4.0-1.module_el8.5.0+890+6b136101.x86_64.rpm 415 kB/s | 70 kB 00:00
(2/13): container-selinux-2.167.0-1.module_el8.5.0+911+f19012f9.noarch.rpm 235 kB/s | 54 kB 00:00
(3/13): fuse-overlayfs-1.7.1-1.module_el8.5.0+890+6b136101.x86_64.rpm 303 kB/s | 73 kB 00:00
(4/13): slirp4netns-1.1.8-1.module_el8.5.0+890+6b136101.x86_64.rpm 681 kB/s | 51 kB 00:00
(5/13): fuse3-libs-3.2.1-12.el8.x86_64.rpm 916 kB/s | 94 kB 00:00
(6/13): fuse-common-3.2.1-12.el8.x86_64.rpm 174 kB/s | 21 kB 00:00
(7/13): fuse3-3.2.1-12.el8.x86_64.rpm 275 kB/s | 50 kB 00:00
(8/13): libcgroup-0.41-19.el8.x86_64.rpm 916 kB/s | 70 kB 00:00
(9/13): docker-ce-20.10.12-3.el8.x86_64.rpm 8.0 MB/s | 22 MB 00:02
(10/13): docker-ce-cli-20.10.12-3.el8.x86_64.rpm 7.2 MB/s | 30 MB 00:04
(11/13): docker-ce-rootless-extras-20.10.12-3.el8.x86_64.rpm 3.1 MB/s | 4.6 MB 00:01
(12/13): docker-scan-plugin-0.12.0-3.el8.x86_64.rpm 10 MB/s | 3.7 MB 00:00
(13/13): containerd.io-1.4.12-3.1.el8.x86_64.rpm 5.1 MB/s | 28 MB 00:05
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total 15 MB/s | 90 MB 00:05
warning: /var/cache/dnf/docker-ce-stable-ab4061364e2cf0db/packages/containerd.io-1.4.12-3.1.el8.x86_64.rpm: Header V4 RSA/SHA512 Signature, key ID 621e9f35: NOKEY
Docker CE Stable - x86_64 59 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
Is this ok [y/N]: y
Key imported successfully
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : docker-scan-plugin-0.12.0-3.el8.x86_64 1/13
Running scriptlet: docker-scan-plugin-0.12.0-3.el8.x86_64 1/13
Installing : docker-ce-cli-1:20.10.12-3.el8.x86_64 2/13
Running scriptlet: docker-ce-cli-1:20.10.12-3.el8.x86_64 2/13
Running scriptlet: container-selinux-2:2.167.0-1.module_el8.5.0+911+f19012f9.noarch 3/13
Installing : container-selinux-2:2.167.0-1.module_el8.5.0+911+f19012f9.noarch 3/13
Running scriptlet: container-selinux-2:2.167.0-1.module_el8.5.0+911+f19012f9.noarch 3/13

Installing : containerd.io-1.4.12-3.1.el8.x86_64 4/13
Running scriptlet: containerd.io-1.4.12-3.1.el8.x86_64 4/13
Running scriptlet: libcgroup-0.41-19.el8.x86_64 5/13
Installing : libcgroup-0.41-19.el8.x86_64 5/13
Running scriptlet: libcgroup-0.41-19.el8.x86_64 5/13
Installing : fuse3-libs-3.2.1-12.el8.x86_64 6/13
Running scriptlet: fuse3-libs-3.2.1-12.el8.x86_64 6/13
Installing : fuse-common-3.2.1-12.el8.x86_64 7/13
Installing : fuse3-3.2.1-12.el8.x86_64 8/13
Installing : fuse-overlayfs-1.7.1-1.module_el8.5.0+890+6b136101.x86_64 9/13
Running scriptlet: fuse-overlayfs-1.7.1-1.module_el8.5.0+890+6b136101.x86_64 9/13
Installing : libslirp-4.4.0-1.module_el8.5.0+890+6b136101.x86_64 10/13
Installing : slirp4netns-1.1.8-1.module_el8.5.0+890+6b136101.x86_64 11/13
Installing : docker-ce-rootless-extras-20.10.12-3.el8.x86_64 12/13
Running scriptlet: docker-ce-rootless-extras-20.10.12-3.el8.x86_64 12/13
Installing : docker-ce-3:20.10.12-3.el8.x86_64 13/13
Running scriptlet: docker-ce-3:20.10.12-3.el8.x86_64 13/13
Running scriptlet: container-selinux-2:2.167.0-1.module_el8.5.0+911+f19012f9.noarch 13/13
Running scriptlet: docker-ce-3:20.10.12-3.el8.x86_64 13/13
Verifying : container-selinux-2:2.167.0-1.module_el8.5.0+911+f19012f9.noarch 1/13
Verifying : fuse-overlayfs-1.7.1-1.module_el8.5.0+890+6b136101.x86_64 2/13
Verifying : libslirp-4.4.0-1.module_el8.5.0+890+6b136101.x86_64 3/13
Verifying : slirp4netns-1.1.8-1.module_el8.5.0+890+6b136101.x86_64 4/13
Verifying : fuse-common-3.2.1-12.el8.x86_64 5/13
Verifying : fuse3-3.2.1-12.el8.x86_64 6/13
Verifying : fuse3-libs-3.2.1-12.el8.x86_64 7/13
Verifying : libcgroup-0.41-19.el8.x86_64 8/13
Verifying : containerd.io-1.4.12-3.1.el8.x86_64 9/13
Verifying : docker-ce-3:20.10.12-3.el8.x86_64 10/13
Verifying : docker-ce-cli-1:20.10.12-3.el8.x86_64 11/13
Verifying : docker-ce-rootless-extras-20.10.12-3.el8.x86_64 12/13
Verifying : docker-scan-plugin-0.12.0-3.el8.x86_64 13/13

Installed:
container-selinux-2:2.167.0-1.module_el8.5.0+911+f19012f9.noarch containerd.io-1.4.12-3.1.el8.x86_64 docker-ce-3:20.10.12-3.el8.x86_64 docker-ce-cli-1:20.10.12-3.el8.x86_64
docker-ce-rootless-extras-20.10.12-3.el8.x86_64 docker-scan-plugin-0.12.0-3.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便轻松安装完成,我们来看看docker的基本信息docker version

1
2
3
4
5
6
7
8
9
10
11
12
[root@VM-12-3-centos ~]# docker version
Client: Docker Engine - Community
Version: 20.10.12
API version: 1.41
Go version: go1.16.12
Git commit: e91ed57
Built: Mon Dec 13 11:45:22 2021
OS/Arch: linux/amd64
Context: default
Experimental: true
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

可以看到Cannot connect to the Docker daemon at unix:///var/run/docker.sock.这样的提示,这是因为刚才安装了,还未启动,这里执行命令启动起来systemctl start docker,此时再来看看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
[root@VM-12-3-centos ~]# docker version
Client: Docker Engine - Community
Version: 20.10.12
API version: 1.41
Go version: go1.16.12
Git commit: e91ed57
Built: Mon Dec 13 11:45:22 2021
OS/Arch: linux/amd64
Context: default
Experimental: true

Server: Docker Engine - Community
Engine:
Version: 20.10.12
API version: 1.41 (minimum version 1.12)
Go version: go1.16.12
Git commit: 459d0df
Built: Mon Dec 13 11:43:44 2021
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.4.12
GitCommit: 7b11cfaabd73bb80907dd23182b9347b4245eb5d
runc:
Version: 1.0.2
GitCommit: v1.0.2-0-g52b36a2
docker-init:
Version: 0.19.0
GitCommit: de40ad0

这里我们还需要将Docker服务设置为开机启动,因为一旦重启系统,Docker服务就被停止了,执行systemctl enable docker 设置开机自动启动

1
2
[root@VM-12-3-centos ~]# systemctl enable docker
Created symlink /etc/systemd/system/multi-user.target.wants/docker.service → /usr/lib/systemd/system/docker.service.

卸载Docker

如果我们不想要Docker,想把它卸载掉,怎么搞呢?这也很方便,执行删除命令

1
yum remove docker-ce docker-ce-cli containerd.io

然后再删除Docker的目录

1
2
rm -rf /var/lib/containerd
rm -rf /var/lib/docker

设置Docker镜像地址

和yun源一样,Docker镜像仓库在国外,这里我们需要配置为国内的代理镜像地址,这里例举几个:

  • https://hub-mirror.c.163.com/
  • https://reg-mirror.qiniu.com/
    其次还有阿里,只不过需要结合自己的ID,这里我就设置为七牛云的吧,先进入到/etc/docker目录,此时,该目录只有一个key.json这个文件,我们需要新建一个daemon.json文件,然后加入如下配置{"registry-mirrors": ["https://reg-mirror.qiniu.com"]},保存重启Docker即可
    1
    2
    systemctl daemon-reload
    systemctl restart docker

Docker实践之Linux安装Docker

https://blogs.52fx.biz/posts/1356988064.html

作者

eyiadmin

发布于

2021-12-13

更新于

2024-05-31

许可协议

评论