0%

Docker安装及使用

Docker安装使用

安装要求

Linux 3.10 版本是在 2013 年 6 月底发布的,我们来看看上述几个 Linux 发行版何时开始使用该内核版本的:

发行版 版本 发布日期 内核版本
CentOS 7.0-1406 2014 年 7 月 3.10.0-123
Debian 8.0 2015 年 4 月 Linux 3.16
Fedora 20 2013 年 12 月 3.11
Ubuntu 13.10 2013 年 10 月 Linux 3.11
可以看到,不同的发行版会选择不同版本的内核。我们来看看现在这些发行版所用的内核版本:
发行版 版本 发布日期 内核版本
CentOS 7.7-1908 2019 年 9 月 3.10.0-1062
CentOS 8.0-1905 2019 年 9 月 4.18.0-80
Debian 10.0 2019 年 7 月 Linux 4.19
Fedora 30 2019 年 4 月 5.0
Ubuntu 19.04 2019 年 4 月 Linux 5.0

可以看到内核版本更新最慢的是 CentOS,而一个很值得注意的点是,Linux 3.10 截至 Linux 3.10.108 就已经 EOL(End Of Life)了,CentOS使用的内核其实移植了很多高版本Linux 中的特性
我们举个实际的例子:Docker 的存储驱动,在早先版本中其实对于 CentOS 系统上一直是默认使用 Devicemapper 的存储驱动,但最近的Docker 版本中已经将 Devicemapper 标记为了废弃。所以我们推荐选择 Overlay2存储驱动。
Docker 的 Overlay2 存储驱动需要内核的 Multiple lower layers 支持,而 OverlayFS 是在 Linux 3.18时合并进入内核的,但在 Linux 3.19 版本时才添加了 Multiple lower layers 的支持。
Linux 3.19 恰好是 3.x 系列的最后一个版本,两个月后 4.0 便发布了,通常情况下,人们会更推荐采用
4.0。所以官方文档上对于使用 Overlay2 存储驱动的建议是使用 Linux 4.0 及更高版本的内核。

而 CentOS 的内核中存在大量的反向移植,Mmultiple lower layers 的特性也被移植到了 CentOS 7.4的内核之上。所以如果你只是考虑使用 Overlay2 作为存储驱动的话,则至少需要选择 CentOS 7.4 内核版本为 3.10.0-693或者更新版本的内核。

安装

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
# 以防你已经安装或者安装失败
yum remove -y docker-ce \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine

# 安装yum软件包
yum install -y yum-utils

# 设置阿里云镜像仓库
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

# 更新软件包索引
yum makecache fast

# 查看yum 版本
yum list docker-ce.x86_64 --showduplicates |sort -r

# 根据需求安装,如果yum安装是直接安装最新版本
yum install docker-ce docker-ce-cli containerd.io

# 使用containerd管理容器(Linux系统内核支持,优化程度相对比较高)
# 最关键是少了一层转化,量少看不出来,量大性能提升明显
# 辑docker配置文件,中间内容如下
mkdir /opt/docker -pv
vim /etc/docker/daemon.json
"exec-opts": ["native.cgroupdriver=systemd"],
"log-driver": "json-file",
"log-opts": {
"max-size": "100m",
"max-file": "10"
},
# 指定 docker 存储文件夹
"data-root": "/opt/docker",
"oom-score-adjust": -1000,
"registry-mirrors": ["http://f1361db2.m.daocloud.io"],
"storage-driver": "overlay2",
"storage-opts":["overlay2.override_kernel_check=true"],
"live-restore": true
}

# 配置Daocloud的镜像加速器,或者你可以配置阿里云的镜像加速器
curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://f1361db2.m.daocloud.io
# 阿里dockerhub镜像加速器,登录阿里云,打开这个链接,然后配置按照阿里云的提示配置即可
https://cr.console.aliyun.com/cn-huhehaote/instances/mirrors



# 启动 docker, 并设置自动启动
systemctl daemon-reload
systemctl enable docker
systemctl start docker
systemctl status docker


# 如果报错 请使用 一般不会有问题的,没有安装成功可能是你之前安装过,导致的问题,可以直接试用于生产环境,我目前全量就跑的是这个
journalctl -fu docker

目前20版本后 dockerd迁移至 containd 如果有调用 2376 端口需要作如下操作

1
2
3
vim /usr/lib/systemd/system/docker.service
# 在原有基础上添加对应端口即可,自定义端口可更改 2376
ExecStart=/usr/bin/dockerd -H fd:// -H tcp://0.0.0.0:2376 --containerd=/run/containerd/containerd.sock

参考连接
https://docs.docker.com/engine/reference/commandline/dockerd/#daemon-configuration-file

如下备忘

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
{
"allow-nondistributable-artifacts": [],
"api-cors-header": "",
"authorization-plugins": [],
"bip": "",
"bridge": "",
"cgroup-parent": "",
"cluster-advertise": "",
"cluster-store": "",
"cluster-store-opts": {},
"containerd": "/run/containerd/containerd.sock",
"containerd-namespace": "docker",
"containerd-plugin-namespace": "docker-plugins",
"data-root": "",
"debug": true,
"default-address-pools": [
{
"base": "172.80.0.0/16",
"size": 24
},
{
"base": "172.90.0.0/16",
"size": 24
}
],
"default-cgroupns-mode": "private",
"default-gateway": "",
"default-gateway-v6": "",
"default-runtime": "runc",
"default-shm-size": "64M",
"default-ulimits": {
"nofile": {
"Hard": 64000,
"Name": "nofile",
"Soft": 64000
}
},
"dns": [],
"dns-opts": [],
"dns-search": [],
"exec-opts": [],
"exec-root": "",
"experimental": false,
"features": {},
"fixed-cidr": "",
"fixed-cidr-v6": "",
"group": "",
"hosts": [],
"icc": false,
"init": false,
"init-path": "/usr/libexec/docker-init",
"insecure-registries": [],
"ip": "0.0.0.0",
"ip-forward": false,
"ip-masq": false,
"iptables": false,
"ip6tables": false,
"ipv6": false,
"labels": [],
"live-restore": true,
"log-driver": "json-file",
"log-level": "",
"log-opts": {
"cache-disabled": "false",
"cache-max-file": "5",
"cache-max-size": "20m",
"cache-compress": "true",
"env": "os,customer",
"labels": "somelabel",
"max-file": "5",
"max-size": "10m"
},
"max-concurrent-downloads": 3,
"max-concurrent-uploads": 5,
"max-download-attempts": 5,
"mtu": 0,
"no-new-privileges": false,
"node-generic-resources": [
"NVIDIA-GPU=UUID1",
"NVIDIA-GPU=UUID2"
],
"oom-score-adjust": -500,
"pidfile": "",
"raw-logs": false,
"registry-mirrors": [],
"runtimes": {
"cc-runtime": {
"path": "/usr/bin/cc-runtime"
},
"custom": {
"path": "/usr/local/bin/my-runc-replacement",
"runtimeArgs": [
"--debug"
]
}
},
"seccomp-profile": "",
"selinux-enabled": false,
"shutdown-timeout": 15,
"storage-driver": "",
"storage-opts": [],
"swarm-default-advertise-addr": "",
"tls": true,
"tlscacert": "",
"tlscert": "",
"tlskey": "",
"tlsverify": true,
"userland-proxy": false,
"userland-proxy-path": "/usr/libexec/docker-proxy",
"userns-remap": ""
}